How to change window title and center widget in Qt?

Hi, I have a problem with changing the window name and central widget in Qt. There is MainWindow:

class MainWindow : public QMainWindow
  {
// (...)
  QStackedWidget* widgets;
  Quiz* widget1, *widget2;
  }

and there is a Quiz class:

class Quiz : public QWidget
  {
  public slots:
    void myClicked();
  }

I wanted to change the MainWindow title after clicking the button, which is a Quiz element (and connected to the myClicked slot).

void Quiz::myClicked()
 {
 static_cast<MainWindow>(parent).myFunction();
 }

void MainWindow::myFunction()
{
widget2 = new Quiz(this,2);
widgets->addWidget(widget2);
std::cout<<"current wdgt: " << widgets->currentIndex() << std::endl; // shows: 0
widgets->setCurrentWidget(widget2);
std::cout<<"current wdgt " << widgets->currentIndex() << std::endl; // shows: 1

setWindowTitle("newTitle");
std::cout<<"Title is " << windowTitle().toStdString() << std::endl;

}

So widgets-> currentIndex shows the index of the new widget, but nothing changes in my window. The same problem is related to the window name - the windowTitle () method returns a new title, but the title in the title bar is old. What for? If I change the name in Quiz :: myClicked by:

parent->setWindowTitle("newTitle");

it works! Why does it work, how weird? Please help.

+4
source share
2

! , ? .

. Qt API. . :

windowTitle: QString

().

, .

: QMainWindow, QDialog, . Windows , . , , QMainWindow.

+2

MainWindow. "" MainWindow, .

, MainWindow::setWindowTitle().

+1

Source: https://habr.com/ru/post/1531965/