Results 1 to 3 of 3

Thread: When does QMainWindow::setCentralWidget() destroy the old central widget?

  1. #1
    Join Date
    May 2008
    Posts
    9
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default When does QMainWindow::setCentralWidget() destroy the old central widget?

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: When does QMainWindow::setCentralWidget() destroy the old central widget?

    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?

  3. The following user says thank you to wysota for this useful post:

    reed (2nd September 2008)

  4. #3
    Join Date
    May 2008
    Posts
    9
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: When does QMainWindow::setCentralWidget() destroy the old central widget?

    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.

Similar Threads

  1. Overlapping Widgets and the Application
    By csvivek in forum Qt Programming
    Replies: 1
    Last Post: 2nd May 2008, 17:32
  2. QDockWidgets without central widget
    By JoeMerchant in forum Qt Programming
    Replies: 17
    Last Post: 8th August 2007, 15:52
  3. Replacing Central Widget in Designer
    By Max Yaffe in forum Qt Tools
    Replies: 2
    Last Post: 11th July 2007, 12:41
  4. paint central widget of a QMainWindow???
    By Shuchi Agrawal in forum Newbie
    Replies: 3
    Last Post: 17th January 2007, 09:02
  5. Central Widget of QMainWindow
    By sumsin in forum Qt Programming
    Replies: 3
    Last Post: 13th March 2006, 19:32

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.