QRadioButton Class

QRadioButton widget 提供一个带有文本标签的单选按钮。更多

Header: #include <QRadioButton>
CMake: find_package(Qt6 REQUIRED COMPONENTS Widgets)
target_link_libraries(mytarget PRIVATE Qt6::Widgets)
qmake: QT += widgets
继承: QAbstractButton

公共函数

QRadioButton(QWidget *parent = nullptr)
QRadioButton(const QString &text, QWidget *parent = nullptr)
virtual ~QRadioButton()

重新实现的公共函数

virtual QSize minimumSizeHint() const override
virtual QSize sizeHint() const override

保护函数

virtual void initStyleOption(QStyleOptionButton *option) const

重新实现的受保护函数

virtual bool event(QEvent *e) override
virtual bool hitButton(const QPoint &pos) const override
virtual void mouseMoveEvent(QMouseEvent *e) override
virtual void paintEvent(QPaintEvent *) override

详细说明

QRadioButton 是一个选项按钮,可以打开(选中)或关闭(不选中)。单选按钮通常为用户提供 "多选一 "的选择。在一组单选按钮中,一次只能选中一个单选按钮;如果用户选择了另一个按钮,则之前选中的按钮将被关闭。

单选按钮默认为自动排他性。如果启用了自动排他性,属于同一父 widget 的单选按钮就会像属于同一个排他性按钮组一样。如果需要为属于同一父 widget 的单选按钮设置多个排他性按钮组,请将它们放入QButtonGroup 中。

每当按钮打开或关闭时,它都会发出toggled() 信号。如果您想在按钮每次改变状态时触发操作,请连接到该信号。使用isChecked() 查看某个按钮是否被选中。

QPushButton 一样,单选按钮也会显示文本和一个小图标。图标用setIcon() 设置。文本可以在构造函数中设置,也可以用setText() 设置。可以在文本中的首选字符前加上 "ampersand",指定快捷键。例如

QRadioButton *button = new QRadioButton("Search from the &cursor", this);

本例中的快捷键是Alt+c。详情请查看QShortcut 文档。要显示实际的 "andpersand",请使用"&&"。

重要继承成员:text(),setText(),text(),setDown(),isDown(),autoRepeat(),group(),setAutoRepeat(),toggle(),pressed(),released(),clicked(), 和toggled().

另请参阅 QPushButton,QToolButton, 和QCheckBox

成员函数文档

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

使用给定的parent 构建单选按钮,但不包含文本或像素图。

parent 参数将传递给QAbstractButton 构造函数。

[explicit] QRadioButton::QRadioButton(const QString &text, QWidget *parent = nullptr)

使用给定的parenttext 字符串构造单选按钮。

parent 参数将传递给QAbstractButton 构造函数。

[virtual noexcept] QRadioButton::~QRadioButton()

毁灭者

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

重实现:QAbstractButton::event(QEvent *e)。

[override virtual protected] bool QRadioButton::hitButton(const QPoint &pos) const

重实现:QAbstractButton::hitButton(const QPoint &pos) const.

[virtual protected] void QRadioButton::initStyleOption(QStyleOptionButton *option) const

使用此QRadioButton 中的值初始化option 。当子类需要QStyleOptionButton ,但又不想自己填写所有信息时,该方法非常有用。

另请参阅 QStyleOption::initFrom()。

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

重构属性访问函数:QWidget::minimumSizeHint

[override virtual protected] void QRadioButton::mouseMoveEvent(QMouseEvent *e)

重实现:QAbstractButton::mouseMoveEvent(QMouseEvent *e)。

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

重实现:QAbstractButton::paintEvent(QPaintEvent *e)。

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

重构属性访问函数:QWidget::sizeHint

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.