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

If you want to create a button use QPushButton.

The QButton class implements an abstract button, and lets subclasses specify how to reply to user actions and how to draw the button.

QButton provides both push and toggle buttons. The QRadioButton and QCheckBox classes provide only toggle buttons; QPushButton and QToolButton provide both toggle and push buttons.

Any button can have either a text or pixmap label. setText() sets the button to be a text button and setPixmap() sets it to be a pixmap button. The text/pixmap is manipulated as necessary to create the "disabled" appearance when the button is disabled.

QButton provides most of the states used for buttons:

isDown() indicates whether the button is pressed down.

isOn() indicates whether the button is on. Only toggle buttons can be switched on and off (see below).

isEnabled() indicates whether the button can be pressed by the user.

setAutoRepeat() sets whether the button will auto-repeat if the user holds it down.

setToggleButton() sets whether the button is a toggle button or not.

The difference between isDown() and isOn() is as follows: When the user clicks a toggle button to toggle it on, the button is first pressed and then released into the on state. When the user clicks it again (to toggle it off), the button moves first to the pressed state, then to the off state (isOn() and isDown() are both FALSE).

Default buttons (as used in many dialogs) are provided by QPushButton::setDefault() and QPushButton::setAutoDefault().

QButton provides five signals: <ol type=1>

pressed() is emitted when the button is pressed. E.g. with the mouse or when animateClick() is called.

released() is emitted when the button is released. E.g. when the mouse is released or the cursor is moved outside the widget.

clicked() is emitted when the button is first pressed and then released when the accelerator key is typed, or when animateClick() is called.

toggled(bool) is emitted when the state of a toggle button changes.

stateChanged(int) is emitted when the state of a tristate toggle button changes.

If the button is a text button with an ampersand (&) in its text, QButton creates an automatic accelerator key. This code creates a push button labelled "Ro<u>c</u>k & Roll" (where the c is underlined). The button gets an automatic accelerator key, Alt+C:

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

In this example, when the user presses Alt+C the button will call animateClick().

You can also set a custom accelerator using the setAccel() function. This is useful mostly for pixmap buttons because they have no automatic accelerator.

p->setPixmap( QPixmap("print.png") );
p->setAccel( ALT+Key_F7 );

All of the buttons provided by Qt (QPushButton, QToolButton, QCheckBox and QRadioButton) can display both text and pixmaps.

To subclass QButton, you must reimplement at least drawButton() (to draw the button's outline) and drawButtonLabel() (to draw its text or pixmap). It is generally advisable to reimplement sizeHint() as well, and sometimes hitButton() (to determine whether a button press is within the button).

To reduce flickering, QButton::paintEvent() sets up a pixmap that the drawButton() function draws in. You should not reimplement paintEvent() for a subclass of QButton unless you want to take over all drawing.

This signal is emitted when the button is activated (i.e. first pressed down and then released when the mouse cursor is inside the button), when the accelerator key is typed or when animateClick() is called. This signal is not emitted if you call setDown().

The QButtonGroup::clicked() signal does the same job, if you want to connect several buttons to the same slot.

Warning: Don't launch a model dialog in response to this signal for a button that has autoRepeat turned on.

Handles paint events for buttons. Small and typically complex buttons are painted double-buffered to reduce flicker. The actually drawing is done in the virtual functions drawButton() and drawButtonLabel().

If the pixmap is monochrome (i.e. it is a QBitmap or its depth is 1) and it does not have a mask, this property will set the pixmap to be its own mask. The purpose of this is to draw transparent bitmaps which are important for toggle buttons, for example.

pixmap() returns 0 if no pixmap was set.

Set this property's value with setPixmap() and get this property's value with pixmap().

This property will return a QString::null if the button has no text. If the text has an ampersand (&) in it, then an accelerator is automatically created for it using the character that follows the '&' as the accelerator key. Any previous accelerator will be overwritten, or cleared if no accelerator is defined by the text.

There is no default text.

Set this property's value with setText() and get this property's value with text().

The definitive Qt documentation is provided in HTML format; it is located at $QTDIR/doc/html and can be read using Qt Assistant or with a web browser. This man page is provided as a convenience for those users who prefer man pages, although this format is not officially supported by Trolltech.

If you find errors in this manual page, please report them to qt-bugs@trolltech.com. Please include the name of the manual page (qbutton.3qt) and the Qt version (3.3.8).