A stacked widget is a container widget that contains other widget. In your settings, you seems to use two stacked widget : a QWebKit widget and a QTreeWidget.
To have them displayed in the QMainWindow
, you have to set the central widget of the QMainWindow
to be the stacked widget and uses QStackedWidget::changeCurrentIndex()
slot to pass from the first widget to the other.
Here is a code sample using a QPushButton
and a QLabel
as elements of the stacked widget.
QMainWindow *mainWindow = new QMainWindow();
QStackedWidget *stackedWidget = new QStackedWidget();
// stacked item 0
QPushButton *pushButton = new QPushButton("Here is a button");
stackedWidget->addItem(pushButton);
// stacked item 1
QLabel *label = new QLabel("Here is a label");
stackedWidget->addItem(label);
// add the stacked widget to the main window
mainWindow->setCentralWidget(stackedWidget);
To change the current displayed item from the button to the label, you can use:
stackedWidget->setCurrentIndex(1); // go to the label widget
stackedWidget->setCurrentIndex(0); // go back to the button widget
Alternative response to the comments. Are you sure you want to use a stacked widget? Actually, stacking widget are specialized to create sort of tab like presentation of widget. If you want to switch from one widget to the other you can use directly the QMainWindow::setCentralWidget() method as seen in the following code.
QMainWindow *mainWindow = new QMainWindow();
QVector< QWidget* > widgets;
// stacked item 0
QPushButton *pushButton = new QPushButton("Here is a button");
// stacked item 1
QLabel *label = new QLabel("Here is a label");
widgets << pushButton << label;
// add the first widget to the main window
mainWindow->setCentralWidget(widgets[0]);
When you want to switch to the other widget, you can use:
mainWindow->setCentralWidget(widgets[1]);