Correct way to replace widgets (without deleting them) in GUI
-
I am having trouble adding/removing and replacing widgets from my GUI.
I have a class MainWindow, that is my main screen and contains the widgets that are being replaced.
Here is how I set up my GUI in the constructor, using a private member variable QGridLayout mainLayout:
mainLayout = new QGridLayout; mainLayout->setMargin(0); mainLayout->setSpacing(0); mainLayout->addWidget(infoPnl, 0, 0, 1, 2); mainLayout->addWidget(contentPnl, 1, 0, 1, 1); mainLayout->addWidget(buttonPnl, 1, 1, 1, 1);
This creates a panel across the top of the window, and two smaller widgets underneath.
I have methods to replace the two panels along the bottom (contentPnl and buttonPnl);
void MainWindow::setContentPane(ContentPanel *content){ mainLayout->replaceWidget(contentPnl, content); contentPnl = content; mainLayout->update(); }
This doesn't work properly, as the new panels overwrite and cover up the existing ones. In this example, when I call setContentPanel(), the content panel is added to the GUI correctly but other elements (such as the infoPnl across the top) are covered.
For some reason, if I add a statement to delete the old widget, this works fine and everything looks proper. I don't want to delete the widget, as I store the GUI components elsewhere and don't want them deleted.
void MainWindow::setContentPane(ContentPanel *content){ mainLayout->replaceWidget(contentPnl, content); delete contentPnl; contentPnl = content; mainLayout->update(); }
Why does deleting the widget change the GUI behavior? How can I achieve this affect without deleting the widget I am replacing?
-
Hi,
Aren't rather looking for something like QStackedWidget ?