QAbstractButton 类

The QAbstractButton 类是按钮 Widget 抽象基类,为按钮提供公共功能。 更多...

头: #include <QAbstractButton>
继承: QWidget
继承者: Q3Button , QCheckBox , QPushButton , QRadioButton ,和 QToolButton

特性

公共函数

QAbstractButton (QWidget * parent = 0)
~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 (int msec = 100)
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 )
virtual bool event (QEvent * e )
virtual void focusInEvent (QFocusEvent * e )
virtual void focusOutEvent (QFocusEvent * e )
virtual void keyPressEvent (QKeyEvent * e )
virtual void keyReleaseEvent (QKeyEvent * e )
virtual void mouseMoveEvent (QMouseEvent * e )
virtual void mousePressEvent (QMouseEvent * e )
virtual void mouseReleaseEvent (QMouseEvent * e )
virtual void paintEvent (QPaintEvent * e ) = 0
virtual void timerEvent (QTimerEvent * e )

额外继承成员

详细描述

The QAbstractButton 类是按钮 Widget 抽象基类,为按钮提供公共功能。

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

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

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

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

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

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

还可以设置自定义快捷键使用 setShortcut () function. This is useful mostly for buttons that do not have any text, because they have no automatic shortcut.

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

All of the buttons provided by Qt ( QPushButton , QToolButton , QCheckBox ,和 QRadioButton ) 均可以显示 text and icons .

A button can be made the default button in a dialog are provided by QPushButton::setDefault () 和 QPushButton::setAutoDefault ().

QAbstractButton 提供用于按钮的大多数状态:

差异在 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

This property holds whether auto-exclusivity is enabled.

若自动独占被启用,属于同一父级 Widget 的可复选按钮的行为就像它们属于同一独占按钮组。在独占按钮组中,任何时候只能复选一按钮;复选另一按钮,自动取消复选先前复选的按钮。

特性对属于按钮组的按钮,不起作用。

默认情况下,autoExclusive (自动独占) 是关闭的,除单选按钮外。

访问函数:

bool autoExclusive () const
void setAutoExclusive (bool)

另请参阅 QRadioButton .

autoRepeat : bool

This property holds whether autoRepeat is enabled.

若 autoRepeat 被启用,那么 pressed (), released (),和 clicked () 信号将按定期间隔发射当按钮被按下时。autoRepeat 默认情况下是关闭的。初始延迟和重复间隔以毫秒为单位定义通过 autoRepeatDelay and autoRepeatInterval .

注意:若按钮被快捷键按下,那么自动重复是被系统而不是此类启用和计时。 pressed (), released (),和 clicked () 信号将像在正常情况下一样被发射。

访问函数:

bool autoRepeat () const
void setAutoRepeat (bool)

autoRepeatDelay : int

This property holds the initial delay of auto-repetition.

autoRepeat 被启用,那么 autoRepeatDelay 定义在自动重复反冲之前的初始延迟 (以毫秒为单位)。

该特性在 Qt 4.2 引入。

访问函数:

int autoRepeatDelay () const
void setAutoRepeatDelay (int)

另请参阅 autoRepeat and autoRepeatInterval .

autoRepeatInterval : int

This property holds the interval of auto-repetition.

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

该特性在 Qt 4.2 引入。

访问函数:

int autoRepeatInterval () const
void setAutoRepeatInterval (int)

另请参阅 autoRepeat and autoRepeatDelay .

checkable : bool

This property holds whether the button is checkable.

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

访问函数:

bool isCheckable () const
void setCheckable (bool)

另请参阅 checked .

checked : bool

This property holds whether the button is checked.

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

访问函数:

bool isChecked () const
void setChecked (bool)

通知程序信号:

void toggled (bool checked )

另请参阅 checkable .

down : bool

This property holds whether the button is pressed down.

If this property is true, the button is pressed down. The signals pressed () 和 clicked () 不被发射若将此特性设为 true。默认为 false。

访问函数:

bool isDown () const
void setDown (bool)

icon : QIcon

This property holds the icon shown on the button.

图标的默认大小由 GUI 样式定义,但可以调节通过设置 iconSize 特性。

访问函数:

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

iconSize : QSize

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

默认大小由 GUI 样式定义。这是图标的最大尺寸。不会按比例放大较小图标。

访问函数:

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

shortcut : QKeySequence

This property holds the mnemonic associated with the button.

访问函数:

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

text : QString

This property holds the text shown on the button.

If the button has no text, the text() function will return a an empty string.

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 = 0)

构造抽象按钮采用 parent .

QAbstractButton:: ~QAbstractButton ()

销毁按钮。

[slot] void QAbstractButton:: animateClick ( int msec = 100)

履行动画点击:立即按下按钮,然后释放在 msec 毫秒后 (默认为 100 ms)。

Calling this function again before the button was released will reset the release timer.

会酌情发射点击关联的所有信号。

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

另请参阅 click ().

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

重实现自 QWidget::changeEvent ().

[virtual protected] void QAbstractButton:: checkStateSet ()

调用此虚拟处理程序当 setChecked () was called, unless it was called from within nextCheckState ()。它允许子类重置它们的中间按钮状态。

另请参阅 nextCheckState ().

[slot] void QAbstractButton:: click ()

履行点击。

与点击关联的所有寻常信号都会适当发射。若按钮可复选,触发按钮状态。

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

另请参阅 animateClick ().

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

This signal is emitted when the button is activated (i.e. pressed down then released while the mouse cursor is inside the button), when the shortcut key is typed, or when click () 或 animateClick () 被调用。显而易见,此信号 not 被发射若调用 setDown (), setChecked () 或 toggle ().

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

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

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

重实现自 QObject::event ().

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

重实现自 QWidget::focusInEvent ().

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

重实现自 QWidget::focusOutEvent ().

QButtonGroup * QAbstractButton:: group () const

返回此按钮所属的组。

若按钮不是成员对于任何 QButtonGroup ,此函数返回 0。

另请参阅 QButtonGroup .

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

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

默认情况下,可点击区域是整个 Widget。子类可以重实现此函数,以提供不同形状和大小的可点击区域支持。

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

重实现自 QWidget::keyPressEvent ().

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

重实现自 QWidget::keyReleaseEvent ().

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

重实现自 QWidget::mouseMoveEvent ().

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

重实现自 QWidget::mousePressEvent ().

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

重实现自 QWidget::mouseReleaseEvent ().

[virtual protected] void QAbstractButton:: nextCheckState ()

调用此虚拟处理程序,当点击按钮时。默认实现调用 setChecked (! isChecked ()) 若按钮 isCheckable ()。它允许子类实现中间按钮状态。

另请参阅 checkStateSet ().

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

重实现自 QWidget::paintEvent ().

[signal] void QAbstractButton:: pressed ()

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

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

[signal] void QAbstractButton:: released ()

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

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

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

重实现自 QObject::timerEvent ().

[slot] void QAbstractButton:: toggle ()

切换可复选按钮的状态。

另请参阅 checked .