Qt logo

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 Slots

Signals

Protected Members


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:

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:

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

Examples: scrollview/scrollview.cpp


Member Function Documentation

void QButton::animateClick () [slot]

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

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

This function does nothing if the button is disabled.

See also: setAccel().

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, or when the accelerator key is typed, or when animateClick() is called.

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 QRadioButton, QCheckBox, QToolButton and QPushButton.

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

Draws the button text or pixmap.

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

Reimplemented in QToolButton, QCheckBox, QRadioButton and QPushButton.

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

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

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

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

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

Reimplemented for internal reasons; the API is not affected.

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.

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

Handles keyboard events for the button.

Space presses the button, the arrow keys cause focus changes.

Reimplemented from QWidget.

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

Reimplemented for internal reasons; the API is not affected.

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.

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

void QButton::setState ( ToggleState t ) [virtual protected]

This protected function sets the button state into state t but does not cause repainting.

See also: setToggleType().

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

void QButton::setToggleType ( ToggleType type ) [virtual protected]

Sets the type of toggling behavior. The default is SingleShot.

Subclasses use this, and present it with a more comfortable interface.

void QButton::stateChanged ( int state ) [signal]

This signal is emitted whenever a toggle button changes status. state is 2 if the button is on, 1 if it is in the "no change" state or 0 if the button is off.

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

See also: clicked().

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-2000 Troll Tech, all rights reserved.


Copyright © 2000 Troll TechTrademarks
Qt version 2.1.0 (pre-release)