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);
}