Results 1 to 7 of 7

Thread: QMainWindow/centralWidget question

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    4
    Thanked 140 Times in 132 Posts

    Default Re: QMainWindow/centralWidget question

    Use QStackedWidget as you central widget and add your widgets to this stacked widget.

  2. The following user says thank you to faldzip for this useful post:

    graciano (14th April 2010)

  3. #2
    Join Date
    Feb 2008
    Posts
    491
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11
    Thanks
    12
    Thanked 142 Times in 135 Posts

    Default Re: QMainWindow/centralWidget question

    From the QMainWindow docs:
    Note: QMainWindow takes ownership of the widget pointer and deletes it at the appropriate time.
    In v2 the destructor of the current form gets called when you select a different form, leaving you with a dangling pointer. Thus the segfault. So in v1 you are not wasting memory because the previous form is deleted.

    Count my vote for QStackedWidget.

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

    graciano (14th April 2010)

  5. #3
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    57
    Thanked 4 Times in 4 Posts

    Default Re: QMainWindow/centralWidget question

    You mean that ...
    Qt Code:
    1. frm000 = new graus000Form;
    To copy to clipboard, switch view to plain text mode 
    ... allocated the memory and the ...
    Qt Code:
    1. this->setCentralWidget(frm000);
    To copy to clipboard, switch view to plain text mode 
    ... tells the QMainWindow to takes ownership of the frm000 pointer.

    So in this case the "appropriate time" happens everytime i set a new central widget and there is no need for any delete frm000.(?)

    Beeing this true the main advantage in using a QStackedWidget will be the speed. And the main disadvantage will be that i will have all widgets loaded in memory while the programs is running ... right?

  6. #4
    Join Date
    Feb 2008
    Posts
    491
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11
    Thanks
    12
    Thanked 142 Times in 135 Posts

    Default Re: QMainWindow/centralWidget question

    So in this case the "appropriate time" happens everytime i set a new central widget and there is no need for any delete frm000.(?)
    yes, no need.
    Beeing this true the main advantage in using a QStackedWidget will be the speed. And the main disadvantage will be that i will have all widgets loaded in memory while the programs is running ... right?
    Another advantage is the simplification of your code. I didn't know the answer to your memory usage question so I set up a stacked widget version of your app. Surprisingly (to me), the QStackedWidget version executable is slightly smaller and uses less memory.

    Executable size:
    • Your v1 = 1,372,139 B
    • Stacked Widget = 1,360,267 B

    pmap results:
    • Your v1 = 26,368K
    • Stacked widget = 26,300K

    I am attaching my .cpp .h .pro & .ui files. Give them a test drive and see if I left something out. - stackwidget.tar..bz2

  7. The following user says thank you to norobro for this useful post:

    graciano (15th April 2010)

  8. #5
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    57
    Thanked 4 Times in 4 Posts

    Default Re: QMainWindow/centralWidget question

    hummm ... interesting thing this QStackedWidget.
    But ... let me "refrase" ...

    If i want to build an application based on a QMainWindow, and i don't want to have my dialogs floating on the user Desktop ... what would be the best approach?
    The idea is to build independent Dialogs for each task ... and then use them in the appropriate moment by showing them in the "centralWidget".

    For now we have:
    1. my v1 example
    2. QStackedWidget
    3. ?

    I think that the MDI approch does not fit in this discussion!?

  9. #6
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    4
    Thanked 140 Times in 132 Posts

    Default Re: QMainWindow/centralWidget question

    [QUOTE=graciano;140033
    I think that the MDI approch does not fit in this discussion!?[/QUOTE]
    It depends how many widgets you can see at a time. MDI allows you to open many windows in MDI area.
    You can use also use QTabWidget. And I didn't know how complicated you 4 widgets are but I think that in your case QStackedWidget is the best solution and memory usage is not such big deal in this case unless you run your app on some PC with 20MB RAM...

  10. The following user says thank you to faldzip for this useful post:

    jaffers (16th August 2010)

Similar Threads

  1. QMainWindow -> centralWidget size
    By lalesculiviu in forum Qt Programming
    Replies: 2
    Last Post: 25th October 2009, 10:40
  2. centralWidget always in center
    By xyfix in forum Qt Programming
    Replies: 3
    Last Post: 29th September 2009, 17:13
  3. QMainWindow->centralWidget with alpha?
    By wafto in forum Newbie
    Replies: 2
    Last Post: 7th December 2008, 03:52
  4. QMainWindow: problem changing centralWidget
    By Caius Aérobus in forum Qt Programming
    Replies: 6
    Last Post: 4th October 2007, 14:00
  5. centralWidget and QDockWidgets in designer
    By momesana in forum Qt Tools
    Replies: 1
    Last Post: 18th December 2006, 12:56

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.