Results 1 to 5 of 5

Thread: Trick for a non destructive QMainWindow::setCentralWidget ?

  1. #1
    Join Date
    Apr 2006
    Posts
    29
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Trick for a non destructive QMainWindow::setCentralWidget ?

    Hello,

    I would very much like my application to have a more or less "dynamic" central widget, at least I wanna be able to switch from a widget, say w1, to another, say w2, without loosing them each time I switch. The problem is QMainWindow::setCentralWidget(QWidget *) deletes the current QMainWindow::centralWidget().

    Here is completely useless code which illustrates the idea.

    Qt Code:
    1. class MainWindow : public QMainWindow
    2. {
    3. ...
    4. public slots:
    5. void switchToW1(void) ;
    6. void switchToW2(void) ;
    7.  
    8. private:
    9. QWidget * w1 ;
    10. QWidget * w2 ;
    11. } ;
    12.  
    13. void MainWindow::switchToW1(void)
    14. {
    15. setCentralWidget(w1) ; // PROBLEM: deletes w2
    16. }
    17.  
    18. void MainWindow::switchToW2(void)
    19. {
    20. setCentralWidget(w2) ; // PROBLEM: deletes w1
    21. }
    To copy to clipboard, switch view to plain text mode 

    I've tried to avoid using setCentralWidget by using a QScrollArea but its QScrollArea::setWidget() method behaves the same way, i.e. it deletes the current widget.

    Of course I end up trying stupid thing like (note this is pseudo code, so of course it does not work syntactically):

    Qt Code:
    1. QWidget * dummy ;
    2. QWidget * w1 = new ... ;
    3. QWidget * w2 = new ... ;
    4.  
    5. setCentralWidget(dummy) ;
    6.  
    7. void MainWindow::switchToW1(void)
    8. {
    9. dummy = w1 ;
    10. update() ;
    11. }
    12.  
    13. void MainWindow::switchToW2(void)
    14. {
    15. dummy = w2 ;
    16. update() ;
    17. }
    To copy to clipboard, switch view to plain text mode 

    But, with no surprise, it does not work either.

    Do anyone has a trick to do something like that correctly ?

    Thanks. Ju.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Trick for a non destructive QMainWindow::setCentralWidget ?

    How about using QStackedWidget as the central widget?

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

    jwintz (6th March 2007)

  4. #3
    Join Date
    Apr 2006
    Posts
    29
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Trick for a non destructive QMainWindow::setCentralWidget ?

    I can't figure why I have a persistent horrible picture in mind of QStackWidget with two horrible arrows at its top left corner that systematically prevents me to consider using it. Terrible confusion.

    Of course, you are right. Thank you.

  5. #4
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Trick for a non destructive QMainWindow::setCentralWidget ?

    Quote Originally Posted by jwintz View Post
    I can't figure why I have a persistent horrible picture in mind of QStackWidget with two horrible arrows at its top left corner that systematically prevents me to consider using it. Terrible confusion.
    Maybe because it's the way it appears in Qt Designer so as to make it easy to navigate through pages... I used to have the same confusion but I tried it in a dialogs and it appeared to be SO nice (and so easy to connect with QComboBox) that I kept it and started to use in many spots where I previously relied on dirty uglt hacks...
    Current Qt projects : QCodeEdit, RotiDeCode

  6. #5
    Join Date
    Apr 2006
    Posts
    29
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Trick for a non destructive QMainWindow::setCentralWidget ?

    That's it ;-) And yes, it is really useful.

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.