Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions

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

Public Slots

Signals

Properties

Protected Members


Detailed Description

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:

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:

  1. pressed() is emitted when the left mouse button is pressed while the mouse cursor is inside the button.
  2. released() is emitted when the left mouse button is released.
  3. clicked() is emitted when the button is first pressed and then released when the accelerator key is typed, or when animateClick() is called.
  4. toggled(bool) is emitted when the state of a toggle button changes.
  5. 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 "Rock & 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.

See also QButtonGroup and Abstract Widget Classes.


Member Type Documentation

QButton::ToggleState

This enum defines the state of a toggle button.

QButton::ToggleType

This enum type defines what a button can do in response to a mouse/keyboard press:


Member Function Documentation

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

Constructs a standard button called name with parent parent, using the widget flags f.

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

QButton::~QButton ()

Destroys the button.

QKeySequence QButton::accel () const

Returns the accelerator associated with the button. See the "accel" property for details.

void QButton::animateClick () [slot]

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

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

This function does nothing if the button is disabled.

See also accel.

bool QButton::autoRepeat () const

Returns TRUE if autoRepeat is enabled; otherwise returns FALSE. See the "autoRepeat" property for details.

bool QButton::autoResize () const

Returns TRUE if autoResize is enabled; otherwise returns FALSE. See the "autoResize" property for details.

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

See also pressed(), released(), toggled(), autoRepeat, and down.

Examples: chart/setdataform.cpp, listbox/listbox.cpp, network/clientserver/client/client.cpp, progressbar/progressbar.cpp, richtext/richtext.cpp, t2/main.cpp, and t4/main.cpp.

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. At some point, these reimplementations should call drawButtonLabel().

See also drawButtonLabel() and paintEvent().

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

Draws the button text or pixmap.

This virtual function is reimplemented by subclasses to draw real buttons. It is invoked by drawButton().

See also drawButton() and paintEvent().

QButtonGroup * QButton::group () const

Returns the group that this button belongs to.

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

See also QButtonGroup.

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

Returns TRUE if pos is inside the clickable button rectangle; otherwise returns FALSE.

By default, the clickable area is the entire widget. Subclasses may reimplement it, though.

bool QButton::isDown () const

Returns TRUE if the button is pressed; otherwise returns FALSE. See the "down" property for details.

bool QButton::isExclusiveToggle () const

Returns TRUE if the button is an exclusive toggle; otherwise returns FALSE. See the "exclusiveToggle" property for details.

bool QButton::isOn () const

Returns TRUE if the button is toggled; otherwise returns FALSE. See the "on" property for details.

bool QButton::isToggleButton () const

Returns TRUE if the button is a toggle button; otherwise returns FALSE. See the "toggleButton" property for details.

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

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

See also drawButton() and drawButtonLabel().

Reimplemented from QWidget.

const QPixmap * QButton::pixmap () const

Returns the pixmap shown on the button. See the "pixmap" property for details.

void QButton::pressed () [signal]

This signal is emitted when the button is pressed down.

See also released() and clicked().

Examples: network/httpd/httpd.cpp and popup/popup.cpp.

void QButton::released () [signal]

This signal is emitted when the button is released.

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

void QButton::setAccel ( const QKeySequence & ) [virtual]

Sets the accelerator associated with the button. See the "accel" property for details.

void QButton::setAutoRepeat ( bool ) [virtual]

Sets whether autoRepeat is enabled. See the "autoRepeat" property for details.

void QButton::setAutoResize ( bool )

Sets whether autoResize is enabled. See the "autoResize" property for details.

void QButton::setDown ( bool ) [virtual]

Sets whether the button is pressed. See the "down" property for details.

void QButton::setOn ( bool on ) [protected]

Sets the state of this button to On if on is TRUE; otherwise to Off.

See also toggleState.

void QButton::setPixmap ( const QPixmap & ) [virtual]

Sets the pixmap shown on the button. See the "pixmap" property for details.

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

Sets the toggle state of the button to s. s can be Off, NoChange or On.

void QButton::setText ( const QString & ) [virtual]

Sets the text shown on the button. See the "text" property for details.

void QButton::setToggleButton ( bool b ) [protected]

If b is TRUE, this button becomes a toggle button; if b is FALSE, this button becomes a command button.

See also toggleButton.

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

Sets the toggle type of the button to type.

type can be set to SingleShot, Toggle and Tristate.

ToggleState QButton::state () const

Returns the state of the toggle button. See the "toggleState" property for details.

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

This signal is emitted whenever a toggle button changes state. state is On if the button is on, NoChange if it is in the "no change" state or Off 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() and QButton::ToggleState.

QString QButton::text () const

Returns the text shown on the button. See the "text" property for details.

void QButton::toggle () [slot]

Toggles the state of a toggle button.

See also on, setOn(), toggled(), and toggleButton.

ToggleType QButton::toggleType () const

Returns the type of toggle on the button. See the "toggleType" property for details.

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

Example: listbox/listbox.cpp.


Property Documentation

QKeySequence accel

This property holds the accelerator associated with the button.

This property is 0 if there is no accelerator set. If you set this property to 0 then any current accelerator is removed.

Set this property's value with setAccel() and get this property's value with accel().

bool autoRepeat

This property holds whether autoRepeat is enabled.

If autoRepeat is enabled then the clicked() signal is emitted at regular intervals if the button is down. This property has no effect on toggle buttons. autoRepeat is off by default.

Set this property's value with setAutoRepeat() and get this property's value with autoRepeat().

bool autoResize

This property holds whether autoResize is enabled.

This property is obsolete. It is provided to keep old source working. We strongly advise against using it in new code.

If autoResize is enabled then the button will resize itself whenever the contents are changed.

Set this property's value with setAutoResize() and get this property's value with autoResize().

bool down

This property holds whether the button is pressed.

If this property is TRUE, the button is pressed down. The signals pressed() and clicked() are not emitted if you set this property to TRUE. The default is FALSE.

Set this property's value with setDown() and get this property's value with isDown().

bool exclusiveToggle

This property holds whether the button is an exclusive toggle.

If this property is TRUE and the button is in a QButtonGroup, the button can only be toggled off by another one being toggled on. The default is FALSE.

Get this property's value with isExclusiveToggle().

bool on

This property holds whether the button is toggled.

This property should only be set for toggle buttons.

Get this property's value with isOn().

QPixmap pixmap

This property holds the pixmap shown on the button.

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

QString text

This property holds the text shown on the button.

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.

There is no default text.

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

bool toggleButton

This property holds whether the button is a toggle button.

The default value is FALSE.

Get this property's value with isToggleButton().

ToggleState toggleState

This property holds the state of the toggle button.

If this property is changed then it does not cause the button to be repainted.

Get this property's value with state().

ToggleType toggleType

This property holds the type of toggle on the button.

The default toggle type is SingleShot.

See also QButton::ToggleType.

Get this property's value with toggleType().


This file is part of the Qt toolkit. Copyright © 1995-2003 Trolltech. All Rights Reserved.


Copyright © 2003 TrolltechTrademarks
Qt version 3.1.2