Results 1 to 7 of 7

Thread: QMainWindow/centralWidget question

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

    Default QMainWindow/centralWidget question

    Hi,

    I created 2 small examples with a QMainWindow that allows me to set the centralWidget using 4 widgets i created: graus000, graus090, graus180 and graus270.

    I have 2 versions: v1 and v2.

    v1 looks like this:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent)
    5. : QMainWindow(parent), ui(new Ui::MainWindow)
    6. {
    7. ui->setupUi(this);
    8. }
    9.  
    10. MainWindow::~MainWindow()
    11. {
    12. delete ui;
    13. }
    14.  
    15. void MainWindow::on_actionVer_a_0_graus_triggered()
    16. {
    17. frm000 = new graus000Form;
    18. this->setCentralWidget(frm000);
    19. }
    20.  
    21. void MainWindow::on_actionVer_a_90_graus_triggered()
    22. {
    23. frm090 = new graus090Form;
    24. this->setCentralWidget(frm090);
    25. }
    26.  
    27. void MainWindow::on_actionVer_a_180_graus_triggered()
    28. {
    29. frm180 = new graus180Form;
    30. this->setCentralWidget(frm180);
    31. }
    32.  
    33. void MainWindow::on_actionVer_a_270_graus_triggered()
    34. {
    35. frm270 = new graus270Form;
    36. this->setCentralWidget(frm270);
    37. }
    To copy to clipboard, switch view to plain text mode 
    This works but it seems i'm wasting memory here!?

    v2 looks like this:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent)
    5. : QMainWindow(parent), ui(new Ui::MainWindow)
    6. {
    7. ui->setupUi(this);
    8. frm000 = new graus000Form;
    9. frm090 = new graus090Form;
    10. frm180 = new graus180Form;
    11. frm270 = new graus270Form;
    12. }
    13.  
    14. MainWindow::~MainWindow()
    15. {
    16. delete ui;
    17. }
    18.  
    19. void MainWindow::on_actionVer_a_0_graus_triggered()
    20. {
    21. this->setCentralWidget(frm000);
    22. }
    23.  
    24. void MainWindow::on_actionVer_a_90_graus_triggered()
    25. {
    26. this->setCentralWidget(frm090);
    27. }
    28.  
    29. void MainWindow::on_actionVer_a_180_graus_triggered()
    30. {
    31. this->setCentralWidget(frm180);
    32. }
    33.  
    34. void MainWindow::on_actionVer_a_270_graus_triggered()
    35. {
    36. this->setCentralWidget(frm270);
    37. }
    To copy to clipboard, switch view to plain text mode 
    I set the centralWidget to graus000, then to othe value and then again to graus000 ... and it crashes.

    Full examples here ( http://www.box.net/shared/ycvua1jarz ).

    What's the best approach for doing what i'm trying to do here?

    Thanks

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

    Default Re: QMainWindow/centralWidget question

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

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

    graciano (14th April 2010)

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

    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.

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

    graciano (14th April 2010)

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

    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?

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

    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

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

    graciano (15th April 2010)

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

    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!?

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

    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...

  11. 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, 09:40
  2. centralWidget always in center
    By xyfix in forum Qt Programming
    Replies: 3
    Last Post: 29th September 2009, 16:13
  3. QMainWindow->centralWidget with alpha?
    By wafto in forum Newbie
    Replies: 2
    Last Post: 7th December 2008, 02:52
  4. QMainWindow: problem changing centralWidget
    By Caius Aérobus in forum Qt Programming
    Replies: 6
    Last Post: 4th October 2007, 13:00
  5. centralWidget and QDockWidgets in designer
    By momesana in forum Qt Tools
    Replies: 1
    Last Post: 18th December 2006, 11: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.