Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt QMainWindow central widget deletion

Tags:

c++

qt

my application requires the user to switch between several screens. The way I'm doing this is by creating different QFrames for each screen, and then setting the Qframes as central widgets on the MainWindow. The problem is that every time I call setCentralWidget(frame), the old frame gets deleted and I can't access it later. How can save that old frame so that I can access it later?

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

like image 369
PTBG Avatar asked Feb 13 '12 20:02

PTBG


2 Answers

You can remove your central widget from QMainWidow reparenting it. Then, you could set new centralWidget;

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

Also using QStackedWidget possibly would be better solution.

like image 123
Lol4t0 Avatar answered Sep 17 '22 23:09

Lol4t0


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

like image 21
Csaba Torda Avatar answered Sep 21 '22 23:09

Csaba Torda