QStackedLayout 类

QStackedLayout 类提供每次仅一 Widget 可见的 Widget 堆栈。 更多...

头: #include <QStackedLayout>
CMake: find_package(Qt6 REQUIRED COMPONENTS Widgets)
target_link_libraries(mytarget PRIVATE Qt6::Widgets)
qmake: QT += widgets
继承: QLayout

公共类型

enum StackingMode { StackOne, StackAll }

特性

公共函数

QStackedLayout ()
QStackedLayout (QWidget * parent )
QStackedLayout (QLayout * parentLayout )
virtual ~QStackedLayout ()
int addWidget (QWidget * widget )
int currentIndex () const
QWidget * currentWidget () const
int insertWidget (int index , QWidget * widget )
void setStackingMode (QStackedLayout::StackingMode stackingMode )
QStackedLayout::StackingMode stackingMode () const
QWidget * widget (int index ) const

重实现公共函数

virtual void addItem (QLayoutItem * item ) override
virtual int count () const override
virtual bool hasHeightForWidth () const override
virtual int heightForWidth (int width ) const override
virtual QLayoutItem * itemAt (int index ) const override
virtual QSize minimumSize () const override
virtual void setGeometry (const QRect & rect ) override
virtual QSize sizeHint () const override
virtual QLayoutItem * takeAt (int index ) override

公共槽

void setCurrentIndex (int index )
void setCurrentWidget (QWidget * widget )

信号

void currentChanged (int index )
void widgetRemoved (int index )

详细描述

QStackedLayout 可用于创建的用户界面类似于某种提供通过 QTabWidget 。还有方便 QStackedWidget 类建立在 QStackedLayout 顶部。

QStackedLayout 可以填充多个子级 Widget (页面)。例如:

    QWidget *firstPageWidget = new QWidget;
    QWidget *secondPageWidget = new QWidget;
    QWidget *thirdPageWidget = new QWidget;
    QStackedLayout *stackedLayout = new QStackedLayout;
    stackedLayout->addWidget(firstPageWidget);
    stackedLayout->addWidget(secondPageWidget);
    stackedLayout->addWidget(thirdPageWidget);
    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addLayout(stackedLayout);
    setLayout(mainLayout);
					

QStackedLayout 没有为用户提供切换页面的内在手段。通常,做到这是透过 QComboBox QListWidget 存储 QStackedLayout 页面的标题。例如:

    QComboBox *pageComboBox = new QComboBox;
    pageComboBox->addItem(tr("Page 1"));
    pageComboBox->addItem(tr("Page 2"));
    pageComboBox->addItem(tr("Page 3"));
    connect(pageComboBox, &QComboBox::activated,
            stackedLayout, &QStackedLayout::setCurrentIndex);
					

当填充布局时,Widget 会被添加到内部列表中。 indexOf () function returns the index of a widget in that list. The widgets can either be added to the end of the list using the addWidget () function, or inserted at a given index using the insertWidget () 函数。 removeWidget () function removes the widget at the given index from the layout. The number of widgets contained in the layout, can be obtained using the count () 函数。

The widget () function returns the widget at a given index position. The index of the widget that is shown on screen is given by currentIndex () and can be changed using setCurrentIndex (). In a similar manner, the currently shown widget can be retrieved using the currentWidget () 函数,和变更使用 setCurrentWidget () 函数。

Whenever the current widget in the layout changes or a widget is removed from the layout, the currentChanged () 和 widgetRemoved () signals are emitted respectively.

另请参阅 QStackedWidget and QTabWidget .

成员类型文档编制

enum QStackedLayout:: StackingMode

This enum specifies how the layout handles its child widgets regarding their visibility.

常量 描述
QStackedLayout::StackOne 0 Only the current widget is visible. This is the default.
QStackedLayout::StackAll 1 All widgets are visible. The current widget is merely raised.

特性文档编制

[read-only] count : const int

This property holds the number of widgets contained in the layout

访问函数:

virtual int count () const override

另请参阅 currentIndex () 和 widget ().

currentIndex : int

此特性保持可见 Widget 的索引位置

当前索引为 -1,若没有当前 Widget。

访问函数:

int currentIndex () const
void setCurrentIndex (int index )

通知程序信号:

void currentChanged (int index )

另请参阅 currentWidget () 和 indexOf ().

stackingMode : StackingMode

determines the way visibility of child widgets are handled.

默认值为 StackOne . Setting the property to StackAll allows you to make use of the layout for overlay widgets that do additional drawing on top of other widgets, for example, graphical editors.

访问函数:

QStackedLayout::StackingMode stackingMode () const
void setStackingMode (QStackedLayout::StackingMode stackingMode )

成员函数文档编制

QStackedLayout:: QStackedLayout ()

Constructs a QStackedLayout with no parent.

This QStackedLayout must be installed on a widget later on to become effective.

另请参阅 addWidget () 和 insertWidget ().

[explicit] QStackedLayout:: QStackedLayout ( QWidget * parent )

Constructs a new QStackedLayout with the given parent .

This layout will install itself on the parent widget and manage the geometry of its children.

[explicit] QStackedLayout:: QStackedLayout ( QLayout * parentLayout )

Constructs a new QStackedLayout and inserts it into the given parentLayout .

[virtual noexcept] QStackedLayout:: ~QStackedLayout ()

销毁此 QStackedLayout . Note that the layout's widgets are not destroyed.

[override virtual] void QStackedLayout:: addItem ( QLayoutItem * item )

重实现: QLayout::addItem (QLayoutItem *item).

int QStackedLayout:: addWidget ( QWidget * widget )

添加给定 widget to the end of this layout and returns the index position of the widget .

QStackedLayout is empty before this function is called, the given widget becomes the current widget.

另请参阅 insertWidget (), removeWidget (),和 setCurrentWidget ().

[signal] void QStackedLayout:: currentChanged ( int index )

This signal is emitted whenever the current widget in the layout changes. The index specifies the index of the new current widget, or -1 if there isn't a new one (for example, if there are no widgets in the QStackedLayout )

注意: 通知程序信号对于特性 currentIndex .

另请参阅 currentWidget () 和 setCurrentWidget ().

QWidget *QStackedLayout:: currentWidget () const

返回当前 Widget,或 nullptr if there are no widgets in this layout.

另请参阅 currentIndex () 和 setCurrentWidget ().

[override virtual] bool QStackedLayout:: hasHeightForWidth () const

重实现: QLayoutItem::hasHeightForWidth() const .

[override virtual] int QStackedLayout:: heightForWidth ( int width ) const

重实现: QLayoutItem::heightForWidth(int) const .

int QStackedLayout:: insertWidget ( int index , QWidget * widget )

插入给定 widget 在给定 index 在此 QStackedLayout 。若 index is out of range, the widget is appended (in which case it is the actual index of the widget that is returned).

QStackedLayout is empty before this function is called, the given widget becomes the current widget.

Inserting a new widget at an index less than or equal to the current index will increment the current index, but keep the current widget.

另请参阅 addWidget (), removeWidget (),和 setCurrentWidget ().

[override virtual] QLayoutItem *QStackedLayout:: itemAt ( int index ) const

重实现: QLayout::itemAt(int index) const .

[override virtual] QSize QStackedLayout:: minimumSize () const

重实现: QLayout::minimumSize() const .

[slot] void QStackedLayout:: setCurrentWidget ( QWidget * widget )

Sets the current widget to be the specified widget . The new current widget must already be contained in this stacked layout.

另请参阅 setCurrentIndex () 和 currentWidget ().

[override virtual] void QStackedLayout:: setGeometry (const QRect & rect )

重实现: QLayout::setGeometry (const QRect &r).

[override virtual] QSize QStackedLayout:: sizeHint () const

重实现: QLayoutItem::sizeHint() const .

[override virtual] QLayoutItem *QStackedLayout:: takeAt ( int index )

重实现: QLayout::takeAt (int index).

QWidget *QStackedLayout:: widget ( int index ) const

返回 Widget 在给定 index ,或 nullptr if there is no widget at the given position.

另请参阅 currentWidget () 和 indexOf ().

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

This signal is emitted whenever a widget is removed from the layout. The widget's index is passed as parameter.

另请参阅 removeWidget ().