6.0.0-rc4Source

QStackedWidget Class

The QStackedWidget class is a stack of widgets of which only one is visible at a time. More…

Header#include <QStackedWidget>
CMaketarget_link_libraries(app PRIVATE Nt::Widgets)
ModuleNt Widgets C++ Classes
InheritsQFrame

List of all members, including inherited members

Public Functions

QStackedWidget(QWidget *parent = nullptr)
~QStackedWidget()
intaddWidget(QWidget *widget)
intinsertWidget(int index, QWidget *widget)
voidremoveWidget(QWidget *widget)
QWidget *widget(int index) const
intindexOf(const QWidget *widget) const
QWidget *currentWidget() const
intcurrentIndex() const
intcount() const

Public Slots

voidsetCurrentIndex(int index)
voidsetCurrentWidget(QWidget *widget)

Signals

voidcurrentChanged(int index)
voidwidgetRemoved(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.

QStackedWidget in the light color scheme

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

[explicit] QStackedWidget::QStackedWidget(QWidget *parent = nullptr)

Constructs a stacked widget with no pages and the given parent.

[override] 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.

[signal] 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().

[slot] void QStackedWidget::setCurrentIndex(int index)

Makes the page at index the current one.

See also currentIndex().

[slot] 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().

[signal] void QStackedWidget::widgetRemoved(int index)

This signal is emitted when the page that was at index is removed.