Classes - Annotated - Tree - Functions - Home - Structure

QPushButton Class Reference

The QPushButton widget provides a command button. More...

#include <qpushbutton.h>

Inherits QButton.

List of all member functions.

Public Members

Public Slots

Important Inherited Members

Properties


Detailed Description

The QPushButton widget provides a command button.

The push button, also referred to as a command button, is perhaps the most central widget in any graphical user interface. Push it to command the computer to perform some action. Typical actions are Ok, Apply, Cancel, Close and Help.

A command button is rectangular (about 80 x 22 pixels) and typically displays a text label describing its action. An underscored character in the label, marked with an ampersand in the text, signals an accelerator key.

This code creates a push button labelled "Rock & Roll". Due to the first ampersand, the c displays underscored and the button gets the automatic accelerator key Alt-C:

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

The text can be changed anytime later with setText(). You can also define a pixmap with setPixmap(). The text/pixmap is manipulated as necessary to create a "disabled" appearance according to the respective GUI style when the button is disabled. In addition to the text or pixmap label, a command button can also display a little icon. Use the extended constructor or setIconSet() to define this icon.

A push button emits the signal clicked() when it is activated with the mouse, the spacebar or a keyboard accelerator. Connect to this signal to perform the button's action. Other signals of less importance are pressed() when the button is pressed down and released() when it is released.

Command buttons in dialogs are by default auto-default buttons, i.e., they become the default push button automatically when they receive the keyboard input focus. A default button is a command button that is activated when the user hits the Enter or Return key in a dialog. Adjust this behaviour with setAutoDefault(). Note that auto-default buttons reserve a little extra space necessary to draw a default-button indicator. If you do not want this space around your buttons, call setAutoDefault(FALSE).

Being so central, the widget has grown to accommodate a great many variations in the past decade. The Microsoft style guide now shows about ten different states of Windows push buttons and the text implies that there are dozens more when all the combinations of features are taken into consideration.

Sorted roughly by importance, the most important modes or states are:

As a general rule, use a push button when the application or dialog window performs an action when the user clicks on it (such as Apply, Cancel, Close and Help) and when the widget is supposed to have a wide, rectangular shape with a text label. Small, typically square buttons that change the state of the window rather than performing an action (such as the buttons in the top-right corner of the QFileDialog) are not command buttons, but tool buttons. Qt provides a special class (QToolButton) for these buttons.

If you need toggle behavior (see setToggleButton()) or a button that auto-repeats the activation signal when being pushed down like the arrows in a scroll bar (see setAutoRepeat()), a command button is probably not what you want. When in doubt, go with a tool button.

A variation of a command button is a menu button. It provides not just one command, but several. Use the method setPopup() to associate a popup menu with a push button.

Other classes of buttons are option buttons (see QRadioButton) and check boxes (see QCheckBox).

In Qt, the QButton class provides most of the modes and other API, and QPushButton provides GUI logic. See QButton for more information about the API.

See also QToolButton, QRadioButton, QCheckBox and GUI Design Handbook: Push Button.


Member Function Documentation

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

Constructs a push button with no text.

The parent and name arguments are sent to the QWidget constructor.

QPushButton::QPushButton ( const QString & text, QWidget * parent, const char * name = 0 )

Constructs a push button with the parent parent and the name name with the text text.

QPushButton::QPushButton ( const QIconSet & icon, const QString & text, QWidget * parent, const char * name = 0 )

Constructs a push button with an icon and a text.

Note that you can also pass a QPixmap object as an icon (thanks to the implicit type conversion C++).

The parent and name arguments are sent to the QWidget constructor.

QPushButton::~QPushButton ()

Destroys the push button

int QButton::accel () const

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

bool QPushButton::autoDefault () const

Returns TRUE if the push button is the auto default button; otherwise returns FALSE. See the "autoDefault" property for details.

bool QButton::autoRepeat () const

Returns TRUE if autoRepeat is enabled; otherwise returns FALSE. See the "autoRepeat" 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.

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

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

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

QButtonGroup * QButton::group () const

Returns a pointer to the group of which this button is a member.

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

See also setGroup() and QButtonGroup.

QIconSet * QPushButton::iconSet () const

Returns the icon set on the push button. See the "iconSet" property for details.

bool QPushButton::isDefault () const

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

bool QButton::isDown () const

Returns TRUE if the button is down (i.e. 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 QPushButton::isFlat () const

Returns TRUE if the border is disabled; otherwise returns FALSE. See the "flat" property for details.

bool QPushButton::isMenuButton () const

Returns TRUE if the push button has a menu button on it; otherwise returns FALSE. See the "menuButton" 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.

const QPixmap * QButton::pixmap () const

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

QPopupMenu * QPushButton::popup () const

Returns the associated popup menu or 0 if no popup menu has been defined.

See also setPopup().

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 ( int ) [virtual]

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

void QPushButton::setAutoDefault ( bool autoDef ) [virtual]

Sets whether the push button is the auto default button to autoDef. See the "autoDefault" property for details.

void QButton::setAutoRepeat ( bool ) [virtual]

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

void QPushButton::setDefault ( bool def ) [virtual]

Sets whether the push button is the default to def. See the "default" property for details.

void QButton::setDown ( bool ) [virtual]

Sets whether the button is down (i.e. pressed). See the "down" property for details.

void QPushButton::setFlat ( bool )

Sets whether the border is disabled. See the "flat" property for details.

void QPushButton::setIconSet ( const QIconSet & )

Sets the icon set on the push button. See the "iconSet" property for details.

void QPushButton::setIsMenuButton ( bool enable ) [virtual]

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

void QPushButton::setOn ( bool enable ) [virtual slot]

Sets whether the push button is toggled to enable. See the "on" property for details.

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

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

void QPushButton::setPopup ( QPopupMenu * popup )

Associates the popup menu popup with this push button and thus turns it into a menu button.

Ownership of the popup menu is not transferred.

See also popup().

Example: qdir/qdir.cpp.

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

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

void QPushButton::setToggleButton ( bool enable )

Sets whether the button is a toggle button to enable. See the "toggleButton" property for details.

ToggleState QButton::state () const

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

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

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, on, toggled() and toggleButton.

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

int accel

This property holds the accelerator associated with the button.

This property will return 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 autoDefault

This property holds whether the push button is the auto default button.

If this property is set to TRUE then the push button will be the focused item in a dialog when it is first shown.

Set this property's value with setAutoDefault() and get this property's value with autoDefault().

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.

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

bool default

This property holds whether the push button is the default.

If this property is set to TRUE then the push button will be pressed if the user hits the enter key in a dialog.

Set this property's value with setDefault() and get this property's value with isDefault().

bool flat

This property holds whether the border is disabled.

Set this property's value with setFlat() and get this property's value with isFlat().

QIconSet iconSet

This property holds the icon set on the push button.

This property will return 0 if there is no icon set on the push button

Set this property's value with setIconSet() and get this property's value with iconSet().

bool menuButton

This property holds whether the push button has a menu button on it.

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

If this property is set to TRUE, then a down arrow is drawn on the push button to indicate that a menu will pop up if the user clicks on the arrow.

Get this property's value with isMenuButton().

bool on

This property holds whether the push button is toggled.

This property should only be set for toggle push buttons.

Set this property's value with setOn().

QPixmap pixmap

This property holds the pixmap shown on the button.

This property will return 0 if the button has no pixmap.

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.

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 null string 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 after the ampersand as the accelerator key.

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.

Set this property's value with setToggleButton().


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


This file is part of the Qt toolkit, copyright © 1995-2001 Trolltech, all rights reserved.


Copyright © 2001 TrolltechTrademarks
Qt version 3.0.0-beta2