Qt Toolkit - QButton Class

# QButton Class Reference

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

`#include <qbutton.h>`

Inherits QWidget.

Inherited by QCheckBox, QPushButton, QRadioButton and QToolButton.

List of all member functions.

## Public Members

- QButton ( QWidget * parent=0, const char * name=0 )
- ~QButton ()
- const char* text () const
- void setText ( const char * )
- const QPixmap* pixmap () const
- void setPixmap ( const QPixmap & )
- int accel () const
- void setAccel ( int )
- bool isToggleButton () const
- void setDown ( bool )
- bool isDown () const
- bool isOn () const
- bool autoResize () const
- void setAutoResize ( bool )
- bool autoRepeat () const
- void setAutoRepeat ( bool )

## Public Slots

- void animateClick ()
- void toggle ()

## Signals

- void pressed ()
- void released ()
- void clicked ()
- void toggled ( bool )

## Protected Members

- void setToggleButton ( bool )
- void setOn ( bool )
- virtual bool hitButton ( const QPoint & pos ) const
- virtual void drawButton ( QPainter * )
- virtual void drawButtonLabel ( QPainter * )
- virtual void keyPressEvent ( QKeyEvent * )
- virtual void mousePressEvent ( QMouseEvent * )
- virtual void mouseReleaseEvent ( QMouseEvent * )
- virtual void mouseMoveEvent ( QMouseEvent * )
- virtual void paintEvent ( QPaintEvent * )
- virtual void focusInEvent ( QFocusEvent * )
- virtual void focusOutEvent ( QFocusEvent * )
- virtual void enabledChange ( bool )

## Detailed Description

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

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 "disabled" appearance when the button is disabled.

QButton provides most of the states used for buttons:

- isDown() determines whether the button is pressed down.
- isOn() determines whether the button is on. Only toggle buttons can be switched on and off (see below).
- isEnabled() determines whether the button can be pressed by the user.
- setAutoRepeat() determines whether the button will auto-repeat if the user holds it down.
- setToggleButton() determines 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, then released into 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 four signals:

- pressed() is emitted when the left mouse button is pressed while the mouse cursor is inside the button.
- released() is emitted when the left mouse button is released.
- clicked() is emitted when the button is first pressed then released, or when the accelerator key is typed, or when animateClick() is called.
- toggled() is emitted when the state of a toggle button changes.

If the button is a text button with "&" in its text, QButton creates an automatic accelerator key. This code creates a push button labelled "Rock & Roll" (where the c is underscored). 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 animate a click.

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

        QPushButton *p;
        p->setPixmap( QPixmap("print.gif") );
        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 have to reimplement at least drawButton() (to draw the button's outskirts) 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 the 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.

See also: QButtonGroup and Microsoft Style Guide

Examples: tictac/tictac.cpp

## Member Function Documentation

### QButton::QButton ( QWidget * parent=0, const char * name=0 )

Constructs a standard button with a parent widget and a name.

If parent is a QButtonGroup, this constructor calls QButtonGroup::insert().

### QButton::~QButton ()

Destroys the button and all its child widgets.

### int QButton::accel () const

Returns the accelerator key currently set for the button, or 0 if no accelerator key has been set.

See also: setAccel().

### void QButton::animateClick () `[slot]`

Performs an animated click: The button is pressed and a short while later released.

pressed(), released(), clicked() and toggled() signals are emitted as appropriate.

This function does nothing if the button is disabled.

See also: setAccel().

### bool QButton::autoRepeat () const

Returns TRUE if the button is auto-repeating, else FALSE.

The default is FALSE.

See also: setAutoRepeat().

### bool QButton::autoResize () const

Returns TRUE if auto-resizing is enabled, or FALSE if auto-resizing is disabled.

Auto-resizing is disabled by default.

See also: setAutoResize().

### void QButton::clicked () `[signal]`

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

See also: pressed(), released() and toggled().

### void QButton::drawButton ( QPainter * ) `[virtual protected]`

Draws the button. The default implementation does nothing.

This virtual function is reimplemented by subclasses to draw real buttons.

Reimplemented in QToolButton, QPushButton, QCheckBox and QRadioButton.

### void QButton::drawButtonLabel ( QPainter * ) `[virtual protected]`

Draws the button text or pixmap. The default implementation does nothing.

This virtual function is reimplemented by subclasses to draw real buttons.

Reimplemented in QToolButton, QPushButton, QCheckBox and QRadioButton.

### void QButton::enabledChange ( bool e ) `[virtual protected]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

### void QButton::focusInEvent ( QFocusEvent * ) `[virtual protected]`

Handles focus in events for the button.

See also: focusOutEvent().

Reimplemented from QWidget.

### void QButton::focusOutEvent ( QFocusEvent * ) `[virtual protected]`

Handles focus out events for the button.

See also: focusInEvent().

Reimplemented from QWidget.

### bool QButton::hitButton ( const QPoint & pos ) const `[virtual protected]`

Returns TRUE if pos is inside the widget rectangle, or FALSE if it is outside.

This virtual function is reimplemented by subclasses.

Reimplemented in QRadioButton.

### bool QButton::isDown () const

Returns TRUE if the button pressed down, or FALSE if it is standing up.

See also: setDown().

Examples: tictac/tictac.cpp

### bool QButton::isOn () const

Returns TRUE if this toggle button is switched on, or FALSE if it is switched off.

See also: setOn() and toggleButton().

Examples: qmag/qmag.cpp

### bool QButton::isToggleButton () const

Returns TRUE if the button is a toggle button.

See also: setToggleButton().

### void QButton::keyPressEvent ( QKeyEvent * e ) `[virtual protected]`

Handles keyboard events for the button.

Space calls animateClick(), the arrow keys cause focus changes.

Reimplemented from QWidget.

### void QButton::mouseMoveEvent ( QMouseEvent * e ) `[virtual protected]`

Handles mouse move events for the button.

See also: mousePressEvent() and mouseReleaseEvent().

Reimplemented from QWidget.

### void QButton::mousePressEvent ( QMouseEvent * e ) `[virtual protected]`

Handles mouse press events for the button.

See also: mouseReleaseEvent().

Reimplemented from QWidget.

### void QButton::mouseReleaseEvent ( QMouseEvent * e ) `[virtual protected]`

Handles mouse release events for the button.

See also: mousePressEvent().

Reimplemented from QWidget.

### void QButton::paintEvent ( QPaintEvent * event ) `[virtual protected]`

Handles paint events for the button.

Opens the painter on the button and calls drawButton().

Reimplemented from QWidget.

### const QPixmap * QButton::pixmap () const

Returns the button pixmap, or 0 if the button has no pixmap.

### void QButton::pressed () `[signal]`

This signal is emitted when the button is pressed down.

See also: released() and clicked().

### void QButton::released () `[signal]`

This signal is emitted when the button is released.

See also: pressed(), clicked() and toggled().

### void QButton::setAccel ( int key )

Specifies an accelerator key for the button, or removes the accelerator if key is 0.

Setting a button text containing a shortcut character (for example the 'x' in E&xit) automatically defines an ALT+letter accelerator for the button. You only need to call this function in order to specify a custom accelerator.

Example:

        QPushButton *b1 = new QPushButton;
        b1->setText( "&OK" );               // sets accel ALT+'O'

        QPushButton *b2 = new QPushButton;
        b2->setPixmap( printIcon );         // pixmap instead of text
        b2->setAccel( CTRL+'P' );           // custom accel

See also: accel(), setText() and QAccel.

### void QButton::setAutoRepeat ( bool enable )

Turns on auto-repeat for the button if enable is TRUE, or turns it off if enable is FALSE.

When auto-repeat is enabled, the clicked() signal is emitted at regular intervals while the buttons is down.

setAutoRepeat() has no effect for toggle buttons.

See also: isDown(), autoRepeat() and clicked().

### void QButton::setAutoResize ( bool enable )

Enables auto-resizing if enable is TRUE, or disables it if enable is FALSE.

When auto-resizing is enabled, the button will resize itself whenever the contents change.

See also: autoResize() and adjustSize().

### void QButton::setDown ( bool enable )

Sets the state of the button to pressed down if enable is TRUE or to standing up if enable is FALSE.

If the button is a toggle button, it is not toggled. Call toggle() as well if you need to do that. The pressed() and released() signals are not emitted by this function.

This method is provided in case you need to override the mouse event handlers.

See also: isDown(), setOn(), toggle() and toggled().

### void QButton::setOn ( bool enable ) `[protected]`

Switches a toggle button on if enable is TRUE or off if enable is FALSE. This function should be called only for toggle buttons.

See also: isOn() and toggleButton().

### void QButton::setPixmap ( const QPixmap & pixmap )

Sets the button to display pixmap and repaints at once.

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

The button resizes itself if auto-resizing is enabled, changes its minimum size if autoMinimumSize() is enabled, and always disables any accelerator.

See also: pixmap(), setText(), setAutoMinimumSize(), setAutoResize(), setAccel() and QPixmap::mask().

### void QButton::setText ( const char * text )

Sets the button to display text and repaints

The button resizes itself if auto-resizing is enabled, changes its minimum size if autoMinimumSize() is enabled, and sets the appropriate accelerator.

See also: text(), setPixmap(), setAutoMinimumSize(), setAutoResize(), setAccel() and QPixmap::mask().

Examples: xform/xform.cpp layout/layout.cpp qmag/qmag.cpp widgets/widgets.cpp

### void QButton::setToggleButton ( bool enable ) `[protected]`

Makes the button a toggle button if enable is TRUE, or a normal button if enable is FALSE.

Note that this function is protected. It is called from subclasses to enable the toggle functionality. QCheckBox and QRadioButton are toggle buttons. QPushButton is initially not a toggle button, but QPushButton::setToggleButton() can be called to create toggle buttons.

See also: isToggleButton().

### const char * QButton::text () const

Returns the button text, or 0 if the button has no text.

See also: setText().

### void QButton::toggle () `[slot]`

if this is a toggle button, toggles it.

### void QButton::toggled ( bool on ) `[signal]`

This signal is emitted whenever a toggle button changes status. on is TRUE if the button is on, or FALSE if the button is off.

This may be the result of a user action, toggle() slot activation, or because setOn() was called.

See also: clicked().

Search the documentation, FAQ, qt-interest archive and more (uses www.troll.no):

This file is part of the Qt toolkit, copyright © 1995-99 Troll Tech, all rights reserved.

It was generated from the following files:

- qbutton.h: 1998/08/21
- qbutton.cpp: 1998/11/01