QGroupBox 类

QGroupBox 小部件提供带有标题的组框框架。 更多...

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

特性

公共函数

QGroupBox (QWidget * parent = nullptr)
QGroupBox (const QString & title , QWidget * parent = nullptr)
virtual ~QGroupBox ()
Qt::Alignment alignment () const
bool isCheckable () const
bool isChecked () const
bool isFlat () const
void setAlignment (int alignment )
void setCheckable (bool checkable )
void setFlat (bool flat )
void setTitle (const QString & title )
QString title () const

重实现公共函数

virtual QSize minimumSizeHint () const override

公共槽

void setChecked (bool checked )

信号

void clicked (bool checked = false)
void toggled (bool on )

保护函数

virtual void initStyleOption (QStyleOptionGroupBox * option ) const

重实现保护函数

virtual void changeEvent (QEvent * ev ) override
virtual void childEvent (QChildEvent * c ) override
virtual bool event (QEvent * e ) override
virtual void focusInEvent (QFocusEvent * fe ) override
virtual void mouseMoveEvent (QMouseEvent * event ) override
virtual void mousePressEvent (QMouseEvent * event ) override
virtual void mouseReleaseEvent (QMouseEvent * event ) override
virtual void paintEvent (QPaintEvent * event ) override
virtual void resizeEvent (QResizeEvent * e ) override

详细描述

组框提供框架、顶部标题、键盘快捷方式,并在其内部显示各种其它 Widget。键盘快捷方式将键盘聚焦移到组框中的某一子级 Widget。

QGroupBox 还允许设置 title (通常在构造函数中设置) 和标题 alignment 。组框可以 checkable 。可复选组框中的子级 Widget 的启用 (或禁用) 从属组框是否 checked .

可以最小化组框的空间消耗,通过启用 flat 特性。对于大多数 styles ,启用此特性会导致移除框架的左边缘、右边缘及底边缘。

QGroupBox 不会自动布置子级 Widget (经常是 QCheckBox QRadioButton ,但可以是任何 Widget)。以下范例展示如何采用布局设置 QGroupBox:

    QGroupBox *groupBox = new QGroupBox(tr("Exclusive Radio Buttons"));
    QRadioButton *radio1 = new QRadioButton(tr("&Radio button 1"));
    QRadioButton *radio2 = new QRadioButton(tr("R&adio button 2"));
    QRadioButton *radio3 = new QRadioButton(tr("Ra&dio button 3"));
    radio1->setChecked(true);
    QVBoxLayout *vbox = new QVBoxLayout;
    vbox->addWidget(radio1);
    vbox->addWidget(radio2);
    vbox->addWidget(radio3);
    vbox->addStretch(1);
    groupBox->setLayout(vbox);
					

另请参阅 QButtonGroup and 组框范例 .

特性文档编制

alignment : Qt::Alignment

此特性保持组框标题的对齐方式。

Most styles place the title at the top of the frame. The horizontal alignment of the title can be specified using single values from the following list:

默认对齐方式为 Qt::AlignLeft .

访问函数:

Qt::Alignment alignment () const
void setAlignment (int alignment )

另请参阅 Qt::Alignment .

checkable : bool

此特性保持组框标题中是否拥有复选框

若此特性为 true , the group box displays its title using a checkbox in place of an ordinary label. If the checkbox is checked, the group box's children are enabled; otherwise, they are disabled and inaccessible.

默认情况下,组框不可复选。

若组框有启用此特性,还会初始校验它,以确保启用其内容。

访问函数:

bool isCheckable () const
void setCheckable (bool checkable )

另请参阅 checked .

checked : bool

此特性保持组框是否被复选

If the group box is checkable, it is displayed with a check box. If the check box is checked, the group box's children are enabled; otherwise, the children are disabled and are inaccessible to the user.

默认情况下,可复选组框还会被复选。

访问函数:

bool isChecked () const
void setChecked (bool checked )

通知程序信号:

void toggled (bool on )

另请参阅 checkable .

flat : bool

此特性保持是扁平化,还是带框架描绘组框

A group box usually consists of a surrounding frame with a title at the top. If this property is enabled, only the top part of the frame is drawn in most styles; otherwise, the whole frame is drawn.

默认情况下,此特性被禁用。即:组框不是扁平化的,除非明确指定。

注意: In some styles, flat and non-flat group boxes have similar representations and may not be as distinguishable as they are in other styles.

访问函数:

bool isFlat () const
void setFlat (bool flat )

另请参阅 title .

title : QString

此特性保持组框标题文本

The group box title text will have a keyboard shortcut if the title contains an ampersand ('&') followed by a letter.

g->setTitle("&User information");
					

在以上范例中, Alt+U 将键盘聚焦移动到组框。见 QShortcut 文档编制了解细节 (要显示实际和号,使用 &&)。

没有标题文本,默认情况下。

访问函数:

QString title () const
void setTitle (const QString & title )

另请参阅 alignment .

成员函数文档编制

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

构造组框 Widget 采用给定 parent 但没有标题。

[explicit] QGroupBox:: QGroupBox (const QString & title , QWidget * parent = nullptr)

构造组框采用给定 title and parent .

[虚拟] QGroupBox:: ~QGroupBox ()

销毁组框。

[override virtual protected] void QGroupBox:: changeEvent ( QEvent * ev )

重实现: QWidget::changeEvent (QEvent *event).

[override virtual protected] void QGroupBox:: childEvent ( QChildEvent * c )

重实现: QObject::childEvent (QChildEvent *event).

[signal] void QGroupBox:: clicked ( bool checked = false)

This signal is emitted when the check box is activated (i.e., pressed down then released while the mouse cursor is inside the button), or when the shortcut key is typed. Notably, this signal is not 被发射若调用 setChecked ().

若复选框被复选, checked 为 true;它为 false,若复选框被取消复选。

另请参阅 checkable , toggled (),和 checked .

[override virtual protected] bool QGroupBox:: event ( QEvent * e )

重实现: QWidget::event (QEvent *event).

[override virtual protected] void QGroupBox:: focusInEvent ( QFocusEvent * fe )

重实现: QWidget::focusInEvent (QFocusEvent *event).

[virtual protected] void QGroupBox:: initStyleOption ( QStyleOptionGroupBox * option ) const

初始化 option 采用值来自此 QGroupBox 。此方法对子类是有用的,当需要 QStyleOptionGroupBox ,但不希望自己填充所有信息。

另请参阅 QStyleOption::initFrom ().

[override virtual] QSize QGroupBox:: minimumSizeHint () const

重实现访问函数为特性: QWidget::minimumSizeHint .

[override virtual protected] void QGroupBox:: mouseMoveEvent ( QMouseEvent * event )

重实现: QWidget::mouseMoveEvent (QMouseEvent *event).

[override virtual protected] void QGroupBox:: mousePressEvent ( QMouseEvent * event )

重实现: QWidget::mousePressEvent (QMouseEvent *event).

[override virtual protected] void QGroupBox:: mouseReleaseEvent ( QMouseEvent * event )

重实现: QWidget::mouseReleaseEvent (QMouseEvent *event).

[override virtual protected] void QGroupBox:: paintEvent ( QPaintEvent * event )

重实现: QWidget::paintEvent (QPaintEvent *event).

[override virtual protected] void QGroupBox:: resizeEvent ( QResizeEvent * e )

重实现: QWidget::resizeEvent (QResizeEvent *event).

[signal] void QGroupBox:: toggled ( bool on )

If the group box is checkable, this signal is emitted when the check box is toggled. on is true if the check box is checked; otherwise, it is false.

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

另请参阅 checkable .