Results 1 to 8 of 8

Thread: (Solved) Removing (replacing) central widget

  1. #1
    Join Date
    Sep 2009
    Location
    Belgrade, Serbia
    Posts
    40
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default (Solved) Removing (replacing) central widget

    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.
    Last edited by frenk_castle; 6th December 2009 at 11:32.

  2. #2
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Removing (replacing) central widget

    Dry coded, here's what I think you could do:

    Qt Code:
    1. QWidget *theWidget = centralWidget();
    2. setCentralWidget(newWidget);
    3. theWidget->deleteLater();
    To copy to clipboard, switch view to plain text mode 
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

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

    Default Re: Removing (replacing) central widget

    QStackedWidget is probably the best option - without a need to delete anything.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    frenk_castle (6th December 2009)

  5. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Removing (replacing) central widget

    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:
    Qt Code:
    1. void QMainWindow::setCentralWidget(QWidget *widget)
    2. {
    3. if (d->layout->centralWidget() && d->layout->centralWidget() != widget) {
    4. d->layout->centralWidget()->hide();
    5. d->layout->centralWidget()->deleteLater();
    6. }
    7. d->layout->setCentralWidget(widget);
    8. }
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to Lykurg for this useful post:

    frenk_castle (6th December 2009)

  7. #5
    Join Date
    Sep 2009
    Location
    Belgrade, Serbia
    Posts
    40
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Removing (replacing) central widget

    @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
    Qt Code:
    1. MainWindow : public QMainWindow
    2. {
    3. //somecode
    4. MyWidget1 *widget1;
    5. MyWidget2 *widget2;
    6. }
    7.  
    8. //someplace in code in MainWindow implementation
    9.  
    10. widget1 = new MyWidget1(this);
    11. setCentralWidget(widget1);
    12.  
    13.  
    14. //some other place in code in MainWindow implementation
    15.  
    16. if(condition)
    17. {
    18. widget2 = new MyWidget(this);
    19. setCentralWidget(widget2);
    20. }
    To copy to clipboard, switch view to plain text mode 

    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.

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

    Default Re: Removing (replacing) central widget

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #7
    Join Date
    Sep 2009
    Location
    Belgrade, Serbia
    Posts
    40
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Removing (replacing) central widget

    Will do. Thank you.

  10. #8
    Join Date
    Nov 2010
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Removing (replacing) central widget

    Thanks guys.
    It worked like charm..
    This is just I am looking for last 1 day.

Similar Threads

  1. Replies: 3
    Last Post: 19th February 2012, 11:40
  2. QDockWidget inside another widget in the center?
    By Antebios in forum Qt Programming
    Replies: 1
    Last Post: 16th February 2010, 07:06
  3. Replies: 2
    Last Post: 23rd March 2009, 17:26
  4. Replies: 2
    Last Post: 7th June 2008, 13:12
  5. Replacing Central Widget in Designer
    By Max Yaffe in forum Qt Tools
    Replies: 2
    Last Post: 11th July 2007, 11:41

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
  •  
Qt is a trademark of The Qt Company.