View Full Version : (Solved) Removing (replacing) central widget

6th December 2009, 07:50
Maybe dumb question but I failed to google the answer so you might help.

I have a MainWindow in my application. And two completely different widgets that I would need to use as central widget. When application starts central widget is not set. When user click new in the file menu a Dialog appears and let him choose one or the other. Depending on his choice I create the appropriate widget and using setCentralWidget make it the central widget of Main Window class.

Now when user finish work and wants to change mode so to speak he click new again he is asked to save his work and the same Dialog appears for him to choose from.

The previous widget needs to be removed as central widget, deleted and new one created and set. I know how to create and set the central widget but how to remove it safely and delete it safely.

So far I had one central widget per application so MainWindow deleted it when he it self was closed.

Thanks in advance.

6th December 2009, 07:55
Dry coded, here's what I think you could do:

QWidget *theWidget = centralWidget();

6th December 2009, 10:00
QStackedWidget is probably the best option - without a need to delete anything.

6th December 2009, 10:00
I haven't understand your use case well, but if you need to change a lot between the to widgets, you can reconsider using QStackedWidget.

And it is enough to set a new widget as central. Qt handles the deletion for you:

void QMainWindow::setCentralWidget(QWidget *widget)
if (d->layout->centralWidget() && d->layout->centralWidget() != widget) {

6th December 2009, 11:24
@wysota, Lykurg. If any of you come to Belgrade please call I owe you guys at least a beer for all the help you provided me. I don't think that I would done half the work I did with out the help from you guys. There is already a message box in all my programs which pops out when you click about. Among general information about the program there is a sentence. This program was made possible by the TrollTech team from Nokia which created the ingenious Qt framework, and generous help by the Qt developers on www.qtcentre.org.

Now back to my problem. I will read documentation on QStackedWidget. To try to better explain myself.

I started to explain better to you what I am trying to do. I wrote a long message and thinking about the problem while I was writing I solved it. I know how to do it.

But just to be clear on one thing let say I have code like this

MainWindow : public QMainWindow
MyWidget1 *widget1;
MyWidget2 *widget2;

//someplace in code in MainWindow implementation

widget1 = new MyWidget1(this);

//some other place in code in MainWindow implementation

widget2 = new MyWidget(this);

Will this cause memory leak if I just leave it like this? Can I delete widget1 after I set widget2 as central or will deleting widget1 cause problems.

Thanks in advance for helping me improve. :)

6th December 2009, 11:28
I think QMainWindow will delete your central widget once you replace it with another one. I really suggest you use QStackedWidget. It does exactly what you want.

6th December 2009, 11:31
Will do. Thank you.

14th December 2010, 10:02
Thanks guys.
It worked like charm..
This is just I am looking for last 1 day. :)