Qt QMainWindow removing a central widget

My application requires the user to switch between multiple screens. I do this by creating different QFrames for each screen, and then setting Qframes as central widgets in MainWindow. The problem is that every time I call setCentralWidget (frame), the old frame is deleted, and I can not access it later. How to save this old frame so that I can access it later?

Please let me know if I am unclear in my question.

+6
source share
3 answers

You can remove the central widget from QMainWidow clearing it again. Then you can install the new CentralWidget;

 QWidget* savedWidget = mainWnd->centralWidget(); savedWidget->setParent(0);//now it is saved mainWnd->setCentralWidget(newWidget); 

Also using a QStackedWidget probably be a better solution.

+9
source

QStackedWidget is an elegant solution to this problem, you can find out how to use it correctly here .

+4
source

You can play with .hide () /. show () in the appropriate shifts to accomplish this. But the best solution for your business will almost certainly be to use QTabWidget or QStackedWidget .

0
source

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


All Articles