0

While using the QtStackedWidget for switching windows in a big project, it doesn't seem to take in consideration the " setWindowTitle " part added in every window, and even for the size it takes only the first size precise in the QtStackedWidget declaration. This is weird.Any clarification I'm here to read.

  • So My question is:

    -does the QtStackedWidget allow us to change the window's title each time we switch the window?

    -and what about the size is it fixed or dynamic?

  • 2
    I understood that you have a QStackedWidget, and you add to it one or more QMainWindows. If this is correct, when you change the visible QMainWindow of your QStackedWidget, its title will not be shown at all, because the title of a QMainWindow is shown only if it is used as a proper window. You should change the title of the QMainWindow that contains the QStackedWidget instead. About the size, it could be a problem related to the layout of your windows, but it is very difficult to tell... Could you prepare a minimal code example? – Gabriella Giordano Jun 17 '18 at 10:04

1 Answers1

0

From the Qt docs setWindowTitle

This property only makes sense for top-level widgets, such as windows and dialogs. If no caption has been set, the title is based of the windowFilePath. If neither of these is set, then the title is an empty string.

You can connect the QStackedWidget signals to the QMainWindow

Here is a working example:

#include <QPushButton>
#include <QHBoxLayout>
#include <QLabel>
#include <QStackedWidget>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{

    QWidget * poCentral = new QWidget(this);
    QVBoxLayout * poVLayout  = new QVBoxLayout;
    QHBoxLayout * poHBtnLayout  = new QHBoxLayout;
    QStackedWidget * poMyStackedWidget = new QStackedWidget(this);
    QPushButton * poNextPage = new QPushButton("Next page", this);

    this->setWindowTitle("Page: 0");

    // switch stacked pages
    connect(poNextPage, &QPushButton::clicked,
            [=]()
    {
        int iPageIndex = poMyStackedWidget->currentIndex() + 1;
        if (iPageIndex >= poMyStackedWidget->count())
        {
            poMyStackedWidget->setCurrentIndex(0);
        }
        else
        {
            poMyStackedWidget->setCurrentIndex(iPageIndex);
        }
        // set window title
        poMyStackedWidget->setWindowTitle(QString("Page: %1").arg(poMyStackedWidget->currentIndex()));
    });

    // Connect the signlas so the main window will display the title.
    connect(poMyStackedWidget, &QStackedWidget::windowTitleChanged,
            this, &MainWindow::setWindowTitle);

    // UI layout
    poHBtnLayout->addWidget(poNextPage);
    poVLayout->addLayout(poHBtnLayout);
    poVLayout->addWidget(poMyStackedWidget);
    poCentral->setLayout(poVLayout);

    // Add dumy pages
    poMyStackedWidget->insertWidget(0,new QLabel("First page", this));
    poMyStackedWidget->insertWidget(1,new QLabel("Second page", this));
    poMyStackedWidget->insertWidget(2,new QLabel("third page", this));

    this->setCentralWidget(poCentral);

}
Simon
  • 1,522
  • 2
  • 12
  • 24