QAbstractButton Class Reference

[ QtGui module]

The QAbstractButton class is the abstract base class of button widgets, providing functionality common to buttons. 更多...

继承 QWidget .

Inherited by QCheckBox , QPushButton , QRadioButton and QToolButton .

方法

Qt Signals


详细描述

The QAbstractButton class is the abstract base class of button widgets, providing functionality common to buttons.

此类实现 abstract button. Subclasses of this class handle user actions, and specify how the button is drawn.

QAbstractButton provides support for both push buttons and checkable (toggle) buttons. Checkable buttons are implemented in the QRadioButton and QCheckBox classes. Push buttons are implemented in the QPushButton and QToolButton classes; these also provide toggle behavior if required.

任何按钮都可以显示包含文本和图标的标签。 setText () sets the text; setIcon () sets the icon. If a button is disabled, its label is changed to give the button a "disabled" appearance.

If the button is a text button with a string containing an ampersand ('&'), QAbstractButton automatically creates a shortcut key. For example:

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

Alt+C shortcut is assigned to the button, i.e., when the user presses Alt+C 按钮会调用 animateClick ()。见 QShortcut documentation for details (to display an actual ampersand, use '&&').

还可以设置自定义快捷键使用 setShortcut () 函数。 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 provides most of the states used for buttons:

差异在 isDown () 和 isChecked () is as follows. When the user clicks a toggle button to check it, the button is first pressed 然后释放到 checked 状态。 When the user clicks it again (to uncheck it), the button moves first to the pressed 状态,然后到 unchecked 状态 ( isChecked () and isDown () are both false).

QAbstractButton 提供 4 个信号:

  1. pressed () is emitted when the left mouse button is pressed while the mouse cursor is inside the button.
  2. released () 是 emitted when the left mouse button is released.
  3. clicked () is emitted when the button is first pressed and then released, when the shortcut key is typed, or when click () 或 animateClick () 是 called.
  4. toggled () is emitted when the state of a toggle button changes.

要子类 QAbstractButton,必须至少重实现 paintEvent () to draw the button's outline and its text or pixmap. It is generally advisable to reimplement sizeHint (),且有时是 hitButton () (to determine whether a button press is within the button). For buttons with more than two states (like tri-state buttons), you will also have to reimplement checkStateSet () 和 nextCheckState ().


方法文档编制

QAbstractButton.__init__ ( self , QWidget   parent  = None)

parent argument, if not None, causes self to be owned by Qt instead of PyQt.

Constructs an abstract button with a parent .

QAbstractButton.animateClick ( self , int  msecs  = 100)

This method is also a Qt slot with the C++ signature void animateClick(int = 100) .

Performs an animated click: the button is pressed immediately, and released msec milliseconds later (the default is 100 ms).

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

All signals associated with a click are emitted as appropriate.

This function does nothing if the button is disabled.

另请参阅 click ().

bool QAbstractButton.autoExclusive ( self )

bool QAbstractButton.autoRepeat ( self )

int QAbstractButton.autoRepeatDelay ( self )

int QAbstractButton.autoRepeatInterval ( self )

QAbstractButton.changeEvent ( self , QEvent   e )

重实现自 QWidget.changeEvent ().

QAbstractButton.checkStateSet ( self )

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

另请参阅 nextCheckState ().

QAbstractButton.click ( self )

This method is also a Qt slot with the C++ signature void 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.

This function does nothing if the button is disabled.

另请参阅 animateClick ().

bool QAbstractButton.event ( self , QEvent   e )

重实现自 QObject.event ().

QAbstractButton.focusInEvent ( self , QFocusEvent   e )

重实现自 QWidget.focusInEvent ().

QAbstractButton.focusOutEvent ( self , QFocusEvent   e )

重实现自 QWidget.focusOutEvent ().

QButtonGroup QAbstractButton.group ( self )

Returns the group that this button belongs to.

If the button is not a member of any QButtonGroup , this function returns 0.

另请参阅 QButtonGroup .

bool QAbstractButton.hitButton ( self , QPoint   pos )

返回 true 若 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.

QIcon QAbstractButton.icon ( self )

QSize QAbstractButton.iconSize ( self )

bool QAbstractButton.isCheckable ( self )

bool QAbstractButton.isChecked ( self )

bool QAbstractButton.isDown ( self )

QAbstractButton.keyPressEvent ( self , QKeyEvent   e )

重实现自 QWidget.keyPressEvent ().

QAbstractButton.keyReleaseEvent ( self , QKeyEvent   e )

重实现自 QWidget.keyReleaseEvent ().

QAbstractButton.mouseMoveEvent ( self , QMouseEvent   e )

重实现自 QWidget.mouseMoveEvent ().

QAbstractButton.mousePressEvent ( self , QMouseEvent   e )

重实现自 QWidget.mousePressEvent ().

QAbstractButton.mouseReleaseEvent ( self , QMouseEvent   e )

重实现自 QWidget.mouseReleaseEvent ().

QAbstractButton.nextCheckState ( self )

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 ().

QAbstractButton.paintEvent ( self , QPaintEvent   e )

This method is abstract and should be reimplemented in any sub-class.

重实现自 QWidget.paintEvent ().

QAbstractButton.setAutoExclusive ( self , bool)

QAbstractButton.setAutoRepeat ( self , bool)

QAbstractButton.setAutoRepeatDelay ( self , int)

QAbstractButton.setAutoRepeatInterval ( self , int)

QAbstractButton.setCheckable ( self , bool)

QAbstractButton.setChecked ( self , bool)

This method is also a Qt slot with the C++ signature void setChecked(bool) .

QAbstractButton.setDown ( self , bool)

QAbstractButton.setIcon ( self , QIcon   icon )

QAbstractButton.setIconSize ( self , QSize   size )

This method is also a Qt slot with the C++ signature void setIconSize(const QSize&) .

QAbstractButton.setShortcut ( self , QKeySequence   key )

QAbstractButton.setText ( self , QString  text )

QKeySequence QAbstractButton.shortcut ( self )

QString QAbstractButton.text ( self )

QAbstractButton.timerEvent ( self , QTimerEvent   e )

重实现自 QObject.timerEvent ().

QAbstractButton.toggle ( self )

This method is also a Qt slot with the C++ signature void toggle() .

Toggles the state of a checkable button.

另请参阅 checked .


Qt Signal Documentation

void clicked (bool = 0)

This is the default overload of this signal.

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 () 被调用。 Notably, this signal is not 被发射若调用 setDown (), setChecked () 或 toggle ().

若按钮是可复选的, checked is true if the button is checked, or false if the button is unchecked.

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

void pressed ()

This is the default overload of this signal.

This signal is emitted when the button is pressed down.

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

void released ()

This is the default overload of this signal.

This signal is emitted when the button is released.

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

void toggled (bool)

This is the default overload of this signal.

This signal is emitted whenever a checkable button changes its 状态。 checked is true if the button is checked, or false if the button is unchecked.

This may be the result of a user action, click () slot activation, or 因为 setChecked () was called.

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 and clicked ().