QStackedLayout Class
The QStackedLayout class lays out widgets so that only one of them is visible at a time. More…
| Header | #include <QStackedLayout> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Widgets) |
| Module | Nt Widgets C++ Classes |
| Inherits | QLayout |
List of all members, including inherited members
Public Functions
| QStackedLayout(QWidget *parent = nullptr) | |
| ~QStackedLayout() | |
| int | addWidget(QWidget *widget) |
| int | insertWidget(int index, QWidget *widget) |
| QWidget * | widget(int index) const |
| QWidget * | currentWidget() const |
| int | currentIndex() const |
Reimplemented Public Functions
| void | addItem(QLayoutItem *item) |
| QLayoutItem * | itemAt(int index) const |
| QLayoutItem * | takeAt(int index) |
| QSize | sizeHint() const |
| QSize | minimumSize() const |
| Qt::Orientations | expandingDirections() const |
| void | setGeometry(const QRect &rect) |
Public Slots
| void | setCurrentIndex(int index) |
| void | setCurrentWidget(QWidget *widget) |
Signals
| void | currentChanged(int index) |
| void | widgetRemoved(int index) |
Detailed Description
All the widgets of a stacked layout have the size of the layout, and the current one is shown while the others are hidden. A QStackedWidget has one of these, and a QComboBox or a list can choose the page:
QStackedLayout *pages = new QStackedLayout(window);
pages->addWidget(generalPage);
pages->addWidget(advancedPage);
connect(combo, &QComboBox::currentIndexChanged, pages, &QStackedLayout::setCurrentIndex);
The size hint of the layout is the largest hint of its widgets, so the window has room for any page.
See also QStackedWidget, QTabWidget and QLayout.
Differences from Qt
There is no stackingMode(): only the current widget is shown, as in Qt's default StackOne. A stacked layout can only hold widgets: addItem() takes the widget of the item. It has no contents margins by default.
Member Function Documentation
QStackedLayout::QStackedLayout(QWidget *parent = nullptr)
Constructs a stacked layout for parent, with no widgets.
QStackedLayout::~QStackedLayout()
Destroys the layout. The widgets that are in it stay.
void QStackedLayout::addItem(QLayoutItem *item)
Reimplements QLayout::addItem().
A stacked layout holds widgets only: the widget of item is added, and item is deleted. An item that is not a widget is ignored.
int QStackedLayout::addWidget(QWidget *widget)
Adds widget at the end of the layout and returns its index. If it is the first widget, it becomes the current one.
See also insertWidget().
void QStackedLayout::currentChanged(int index)
This signal is emitted when the current widget changes, with its index, or -1 if there is none.
int QStackedLayout::currentIndex() const
Returns the index of the current widget, or -1 if there is none.
See also setCurrentIndex().
QWidget * QStackedLayout::currentWidget() const
Returns the current widget, or nullptr if there is none.
See also currentIndex().
Qt::Orientations QStackedLayout::expandingDirections() const
Reimplements QLayout::expandingDirections().
int QStackedLayout::insertWidget(int index, QWidget *widget)
Inserts widget at index, which is brought into the range 0 to count(), and returns the index it got. It becomes the current widget if there is none; otherwise it is hidden. The layout makes it a child of the widget it manages.
See also addWidget().
QLayoutItem * QStackedLayout::itemAt(int index) const
Reimplements QLayout::itemAt().
QSize QStackedLayout::minimumSize() const
Reimplements QLayout::minimumSize().
void QStackedLayout::setCurrentIndex(int index)
Makes the widget at index the current one: it is shown and the one before is hidden. Does nothing if there is no widget at index. Emits currentChanged().
See also currentIndex().
void QStackedLayout::setCurrentWidget(QWidget *widget)
Makes widget the current one. Does nothing if it is not in the layout.
See also setCurrentIndex().
void QStackedLayout::setGeometry(const QRect &rect)
Reimplements QLayout::setGeometry().
Gives the current widget the whole rectangle, within the margins.
QSize QStackedLayout::sizeHint() const
Reimplements QLayout::sizeHint().
QLayoutItem * QStackedLayout::takeAt(int index)
Reimplements QLayout::takeAt().
If the widget was the current one, the next widget becomes the current one, or the one before if it was the last. Emits widgetRemoved(), and currentChanged() if the current widget was the one removed.
QWidget * QStackedLayout::widget(int index) const
Returns the widget at index, or nullptr if there is none.
void QStackedLayout::widgetRemoved(int index)
This signal is emitted when the widget that was at index is removed from the layout.