PDA

View Full Version : When does QMainWindow::setCentralWidget() destroy the old central widget?



reed
6th June 2008, 21:11
When does QMainWindow::setCentralWidget() destroy the old central widget? I'm calling setCentralWidget() from a slot that's a method of the old central widget, and it's crashing. Should I not do that?

Instead, I guess I'd create a wrapper or container widget that remains the central widget forever, and manages the switch. Or perhaps instead of calling setCentralWidget() directy, emit a signal caught by some other object (subclass QMainWindow?) to do the change?

Thanks

Reed

wysota
6th June 2008, 22:08
The old widget gets deleted immediately when you set the new one and you can't delete an object that is source or an argument to a signal that is currently processed because there might be other slots connected to the same signal that might want to do something with the object, so if you delete it, they operate on an invalid pointer. I don't know what you are doing but maybe you should use QStackedWidget?

reed
7th June 2008, 13:12
Thanks. I'm creating a new widget in the slot to replace the old one, and I don't want to keep the old widget around. Does Qt use reference counting or something to determine when to delete a widget?

Well, I think you confirmed what I thought was going on, I have some ways to deal with it.