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.