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.
{
...
public slots:
void switchToW1(void) ;
void switchToW2(void) ;
private:
} ;
void MainWindow::switchToW1(void)
{
setCentralWidget(w1) ; // PROBLEM: deletes w2
}
void MainWindow::switchToW2(void)
{
setCentralWidget(w2) ; // PROBLEM: deletes w1
}
class MainWindow : public QMainWindow
{
...
public slots:
void switchToW1(void) ;
void switchToW2(void) ;
private:
QWidget * w1 ;
QWidget * w2 ;
} ;
void MainWindow::switchToW1(void)
{
setCentralWidget(w1) ; // PROBLEM: deletes w2
}
void MainWindow::switchToW2(void)
{
setCentralWidget(w2) ; // PROBLEM: deletes w1
}
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):
setCentralWidget(dummy) ;
void MainWindow::switchToW1(void)
{
dummy = w1 ;
update() ;
}
void MainWindow::switchToW2(void)
{
dummy = w2 ;
update() ;
}
QWidget * dummy ;
QWidget * w1 = new ... ;
QWidget * w2 = new ... ;
setCentralWidget(dummy) ;
void MainWindow::switchToW1(void)
{
dummy = w1 ;
update() ;
}
void MainWindow::switchToW2(void)
{
dummy = w2 ;
update() ;
}
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.
Bookmarks