QStackedWidget Class
The QStackedWidget class is a stack of widgets of which only one is visible at a time. More…
| Header | #include <QStackedWidget> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Widgets) |
| Module | Nt Widgets C++ Classes |
| Inherits | QFrame |
List of all members, including inherited members
Public Functions
| QStackedWidget(QWidget *parent = nullptr) | |
| ~QStackedWidget() | |
| int | addWidget(QWidget *widget) |
| int | insertWidget(int index, QWidget *widget) |
| void | removeWidget(QWidget *widget) |
| QWidget * | widget(int index) const |
| int | indexOf(const QWidget *widget) const |
| QWidget * | currentWidget() const |
| int | currentIndex() const |
| int | count() const |
Public Slots
| void | setCurrentIndex(int index) |
| void | setCurrentWidget(QWidget *widget) |
Signals
| void | currentChanged(int index) |
| void | widgetRemoved(int index) |
Detailed Description
A stacked widget has pages, which are widgets, and shows the current page at the size of the stacked widget. It has no controls of its own to change the page: connect it to something that has, such as a QComboBox or a row of buttons. A QTabWidget is a stack with a tab bar.
QStackedWidget *stack = new QStackedWidget(window);
stack->addWidget(new QLabel("The first page"));
stack->addWidget(new QLabel("The second page"));
connect(combo, &QComboBox::currentIndexChanged, stack, &QStackedWidget::setCurrentIndex);
The pages of a stacked widget: the first is current, the second hidden.

See also QStackedLayout and QTabWidget.
Differences from Qt
The pages fill the whole widget; the frame of a QFrame, if there is one, is drawn behind them and does not take any of their room.
Member Function Documentation
QStackedWidget::QStackedWidget(QWidget *parent = nullptr)
Constructs a stacked widget with no pages and the given parent.
QStackedWidget::~QStackedWidget()
Destroys the stacked widget, and the pages.
int QStackedWidget::addWidget(QWidget *widget)
Adds widget as a page after the last one and returns its index. The first page added becomes the current one.
See also insertWidget().
int QStackedWidget::count() const
Returns the number of pages.
void QStackedWidget::currentChanged(int index)
This signal is emitted when the current page changes, with its index, or -1 if there is none.
int QStackedWidget::currentIndex() const
Returns the index of the current page, or -1 if there is none.
See also setCurrentIndex().
QWidget * QStackedWidget::currentWidget() const
Returns the current page, or nullptr if there is none.
See also currentIndex().
int QStackedWidget::indexOf(const QWidget *widget) const
Returns the index of widget, or -1 if it is not a page.
See also widget().
int QStackedWidget::insertWidget(int index, QWidget *widget)
Inserts widget as a page at index, and returns the index it got.
See also addWidget().
void QStackedWidget::removeWidget(QWidget *widget)
Removes widget from the stack. The widget is not deleted, and stays a child of the stacked widget; it is hidden.
See also addWidget().
void QStackedWidget::setCurrentIndex(int index)
Makes the page at index the current one.
See also currentIndex().
void QStackedWidget::setCurrentWidget(QWidget *widget)
Makes widget the current page. Does nothing if it is not a page.
See also setCurrentIndex().
QWidget * QStackedWidget::widget(int index) const
Returns the page at index, or nullptr if there is none.
See also indexOf().
void QStackedWidget::widgetRemoved(int index)
This signal is emitted when the page that was at index is removed.