PTBG
PTBG

Reputation: 595

Qt QMainWindow central widget deletion

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.

Upvotes: 6

Views: 6240

Answers (3)

Csaba Torda
Csaba Torda

Reputation: 96

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

Upvotes: 4

Lol4t0
Lol4t0

Reputation: 12557

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.

Upvotes: 11

jkerian
jkerian

Reputation: 17046

You can play around with .hide()/.show() on the appropriate subwidgets to accomplish this. But a better solution for your case is almost certainly to use a QTabWidget or QStackedWidget.

Upvotes: 0

Related Questions