QAbstractButton 类

QAbstractButton 类是按钮 Widget 的抽象基类,提供按钮共有功能。 更多...

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

QCheckBox , QPushButton , QRadioButton ,和 QToolButton

特性

公共函数

QAbstractButton (QWidget * parent = nullptr)
virtual ~QAbstractButton ()
bool autoExclusive () const
bool autoRepeat () const
int autoRepeatDelay () const
int autoRepeatInterval () const
QButtonGroup * group () const
QIcon icon () const
QSize iconSize () const
bool isCheckable () const
bool isChecked () const
bool isDown () const
void setAutoExclusive (bool)
void setAutoRepeat (bool)
void setAutoRepeatDelay (int)
void setAutoRepeatInterval (int)
void setCheckable (bool)
void setDown (bool)
void setIcon (const QIcon & icon )
void setShortcut (const QKeySequence & key )
void setText (const QString & text )
QKeySequence shortcut () const
QString text () const

公共槽

void animateClick ()
void click ()
void setChecked (bool)
void setIconSize (const QSize & size )
void toggle ()

信号

void clicked (bool checked = false)
void pressed ()
void released ()
void toggled (bool checked )

保护函数

virtual void checkStateSet ()
virtual bool hitButton (const QPoint & pos ) const
virtual void nextCheckState ()

重实现保护函数

virtual void changeEvent (QEvent * e ) override
virtual bool event (QEvent * e ) override
virtual void focusInEvent (QFocusEvent * e ) override
virtual void focusOutEvent (QFocusEvent * e ) override
virtual void keyPressEvent (QKeyEvent * e ) override
virtual void keyReleaseEvent (QKeyEvent * e ) override
virtual void mouseMoveEvent (QMouseEvent * e ) override
virtual void mousePressEvent (QMouseEvent * e ) override
virtual void mouseReleaseEvent (QMouseEvent * e ) override
virtual void paintEvent (QPaintEvent * e ) override = 0
virtual void timerEvent (QTimerEvent * e ) override

详细描述

此类实现 abstract 按钮。此类的子类处理用户动作,并指定如何绘制按钮。

QAbstractButton 提供按钮和可复选 (触发) 按钮支持。可复选按钮的实现在 QRadioButton and QCheckBox 类。按钮的实现在 QPushButton and QToolButton 类;这些还提供触发行为若要求的话。

任何按钮都可以显示包含文本和图标的标签。 setText () 设置文本; setIcon () 设置图标。若按钮被禁用,将改变其标签以赋予按钮禁用外观。

若按钮是带有包含和号 & 的字符串的文本按钮,QAbstractButton 会自动创建快捷键。例如:

QPushButton *button = new QPushButton(tr("Ro&ck && Roll"), this);
							

Alt+C 快捷方式被赋值给按钮,即:当用户按下 Alt+C 按钮会调用 animateClick ()。见 QShortcut 文档编制了解细节。要显示实际和号,使用 &&。

还可以设置自定义快捷键使用 setShortcut () 函数。这对于没有任何文本的按钮非常有用,因此不可以有任何自动快捷方式。

button->setIcon(QIcon(":/images/print.png"));
button->setShortcut(tr("Alt+F7"));
							

由 Qt 提供的所有按钮 ( QPushButton , QToolButton , QCheckBox ,和 QRadioButton ) 均可以显示 text and icons .

可以把按钮设为对话框默认按钮通过 QPushButton::setDefault () 和 QPushButton::setAutoDefault ().

QAbstractButton 为按钮提供大多数使用状态:

  • isDown () 指示按钮是否 pressed down.
  • isChecked () 指示按钮是否 checked 。只有可复选按钮可以被复选或取消复选 (见下文)。
  • isEnabled () 指示用户是否可以按下按钮。

    注意: 与其它 Widget 相反,派生自 QAbstractButton 的按钮接受鼠标和上下文菜单事件当禁用时。

  • setAutoRepeat () 设置按钮是否会自动重复若用户按下按钮。 autoRepeatDelay and autoRepeatInterval 定义自动重复如何做。
  • setCheckable () 设置按钮是否为可触发按钮。

差异在 isDown () 和 isChecked () 如下所示。当用户点击触发按钮以复选时,按钮首先 pressed 然后释放到 checked 状态。当用户再次点击它 (以取消复选) 时,按钮先被移到 pressed 状态,然后到 unchecked 状态 ( isChecked () 和 isDown () 两者为 false)。

QAbstractButton 提供 4 个信号:

  1. pressed () 被发射当鼠标光标在按钮内时按下鼠标左键。
  2. released () 被发射当释放鼠标左键时。
  3. clicked () 被发射当首次按下然后释放按钮时,当键入快捷键时或当 click () 或 animateClick () 被调用。
  4. toggled () 被发射当触发按钮状态改变时。

要子类 QAbstractButton,必须至少重实现 paintEvent () 以绘制按钮轮廓及其文本或像素图。通常明智的是重实现 sizeHint (),且有时是 hitButton () (以确定按钮按下是否在按钮内)。对于具有 2 种以上状态的按钮 (像 3 状态按钮),还必须重实现 checkStateSet () 和 nextCheckState ().

另请参阅 QButtonGroup .

特性文档编制

autoExclusive : bool

此特性保持是否启用自动排它性

If auto-exclusivity is enabled, checkable buttons that belong to the same parent widget behave as if they were part of the same exclusive button group. In an exclusive button group, only one button can be checked at any time; checking another button automatically unchecks the previously checked one.

The property has no effect on buttons that belong to a button group.

默认情况下,autoExclusive 是关闭的,单选按钮除外。

访问函数:

bool autoExclusive () const
void setAutoExclusive (bool)

另请参阅 QRadioButton .

autoRepeat : bool

此特性保持是否启用 autoRepeat

若 autoRepeat 被启用,那么 pressed (), released (),和 clicked () signals are emitted at regular intervals when the button is down. autoRepeat is off by default. The initial delay and the repetition interval are defined in milliseconds by autoRepeatDelay and autoRepeatInterval .

Note: If a button is pressed down by a shortcut key, then auto-repeat is enabled and timed by the system and not by this class. The pressed (), released (),和 clicked () signals will be emitted like in the normal case.

访问函数:

bool autoRepeat () const
void setAutoRepeat (bool)

autoRepeatDelay : int

此特性保持自动重复的初始延迟

autoRepeat is enabled, then autoRepeatDelay defines the initial delay in milliseconds before auto-repetition kicks in.

访问函数:

int autoRepeatDelay () const
void setAutoRepeatDelay (int)

另请参阅 autoRepeat and autoRepeatInterval .

autoRepeatInterval : int

此特性保持自动重复的间隔

autoRepeat 被启用,autoRepeatInterval 将定义自动重复间隔的长度 (以毫秒为单位)。

访问函数:

int autoRepeatInterval () const
void setAutoRepeatInterval (int)

另请参阅 autoRepeat and autoRepeatDelay .

checkable : bool

此特性保持按钮是否可复选

默认情况下,按钮是不可复选的。

访问函数:

bool isCheckable () const
void setCheckable (bool)

另请参阅 checked .

checked : bool

此特性保持按钮是否被复选

仅可复选按钮才可以被复选。默认情况下,按钮是取消复选的。

访问函数:

bool isChecked () const
void setChecked (bool)

通知程序信号:

void toggled (bool checked )

另请参阅 checkable .

down : bool

此特性保持按钮是否被按下

若此特性为 true ,按钮被按下。信号 pressed () 和 clicked () 不被发射若将此特性设为 true。默认为 false。

访问函数:

bool isDown () const
void setDown (bool)

icon : QIcon

此特性保持在按钮上展示的图标

The icon's default size is defined by the GUI style, but can be adjusted by setting the iconSize 特性。

访问函数:

QIcon icon () const
void setIcon (const QIcon & icon )

iconSize : QSize

此特性保持用于此按钮的图标尺寸。

The default size is defined by the GUI style. This is a maximum size for the icons. Smaller icons will not be scaled up.

访问函数:

QSize iconSize () const
void setIconSize (const QSize & size )

shortcut : QKeySequence

此特性保持按钮关联的助记键

访问函数:

QKeySequence shortcut () const
void setShortcut (const QKeySequence & key )

text : QString

此特性保持按钮中展示的文本

若按钮没有文本,text() 函数将返回空字符串。

If the text contains an ampersand character ('&'), a shortcut is automatically created for it. The character that follows the '&' will be used as the shortcut key. Any previous shortcut will be overwritten or cleared if no shortcut is defined by the text. See the QShortcut 文档编制了解细节。要显示实际和号,使用 &&。

没有默认文本。

访问函数:

QString text () const
void setText (const QString & text )

成员函数文档编制

QAbstractButton:: QAbstractButton ( QWidget * parent = nullptr)

构造抽象按钮采用 parent .

[slot] void QAbstractButton:: animateClick ()

Performs an animated click: the button is pressed immediately, and released 100ms later.

Calling this function again before the button is released resets the release timer.

All signals associated with a click are emitted as appropriate.

此函数什么都不做若按钮 disabled.

另请参阅 click ().

[slot] void QAbstractButton:: click ()

履行点击。

All the usual signals associated with a click are emitted as appropriate. If the button is checkable, the state of the button is toggled.

此函数什么都不做若按钮 disabled.

另请参阅 animateClick ().

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

此信号被发射当被激活 (即:按下然后释放当鼠标光标在按钮内时) 时,当键入快捷键时,或当 click () 或 animateClick () 被调用。显而易见,此信号 not 被发射若调用 setDown (), setChecked () 或 toggle ().

若按钮是可复选的, checked 为 true 若按钮被复选,或 false 若按钮未被复选。

另请参阅 pressed (), released (),和 toggled ().

[signal] void QAbstractButton:: pressed ()

此信号被发射当按钮被按下。

另请参阅 released () 和 clicked ().

[signal] void QAbstractButton:: released ()

此信号被发射当按钮被释放。

另请参阅 pressed (), clicked (),和 toggled ().

[slot] void QAbstractButton:: toggle ()

切换可复选按钮的状态。

另请参阅 checked .

[signal] void QAbstractButton:: toggled ( bool checked )

This signal is emitted whenever a checkable button changes its state. checked 为 true 若按钮被复选,或 false 若按钮未被复选。

This may be the result of a user action, click () slot activation, or because setChecked () 被调用。

The states of buttons in exclusive button groups are updated before this signal is emitted. This means that slots can act on either the "off" signal or the "on" signal emitted by the buttons in the group whose states have changed.

For example, a slot that reacts to signals emitted by newly checked buttons but which ignores signals from buttons that have been unchecked can be implemented using the following pattern:

void MyWidget::reactToToggle(bool checked)
{
   if (checked) {
      // Examine the new button states.
      ...
   }
}
							

Button groups can be created using the QButtonGroup class, and updates to the button states monitored with the QButtonGroup::buttonClicked () 信号。

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

另请参阅 checked and clicked ().

[virtual] QAbstractButton:: ~QAbstractButton ()

Destroys the button.

[override virtual protected] void QAbstractButton:: changeEvent ( QEvent * e )

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

[virtual protected] void QAbstractButton:: checkStateSet ()

This virtual handler is called when setChecked () is used, unless it is called from within nextCheckState (). It allows subclasses to reset their intermediate button states.

另请参阅 nextCheckState ().

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

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

[override virtual protected] void QAbstractButton:: focusInEvent ( QFocusEvent * e )

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

[override virtual protected] void QAbstractButton:: focusOutEvent ( QFocusEvent * e )

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

QButtonGroup *QAbstractButton:: group () const

返回此按钮所属的组。

If the button is not a member of any QButtonGroup ,此函数返回 nullptr .

另请参阅 QButtonGroup .

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

返回 true if pos is inside the clickable button rectangle; otherwise returns false .

By default, the clickable area is the entire widget. Subclasses may reimplement this function to provide support for clickable areas of different shapes and sizes.

[override virtual protected] void QAbstractButton:: keyPressEvent ( QKeyEvent * e )

重实现: QWidget::keyPressEvent (QKeyEvent *event).

[override virtual protected] void QAbstractButton:: keyReleaseEvent ( QKeyEvent * e )

重实现: QWidget::keyReleaseEvent (QKeyEvent *event).

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

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

[override virtual protected] void QAbstractButton:: mousePressEvent ( QMouseEvent * e )

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

[override virtual protected] void QAbstractButton:: mouseReleaseEvent ( QMouseEvent * e )

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

[virtual protected] void QAbstractButton:: nextCheckState ()

This virtual handler is called when a button is clicked. The default implementation calls setChecked (! isChecked ()) if the button isCheckable (). It allows subclasses to implement intermediate button states.

另请参阅 checkStateSet ().

[override pure virtual protected] void QAbstractButton:: paintEvent ( QPaintEvent * e )

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

[override virtual protected] void QAbstractButton:: timerEvent ( QTimerEvent * e )

重实现: QObject::timerEvent (QTimerEvent *event).