The QPushButton widget provides a command button. More...
#include <qpushbutton.h>
Inherits QButton.
The push button, also referred to as 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 or Help.
A command button is rectangular (ca. 80x22 pixel) 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 "disabled" appearance according to the respective GUI style when the button is disabled. A command button can, in addition to the text or pixmap label, 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, either 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, respectively.
Command buttons 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 users 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 accomodate a great many variations in the past decade, and by now the Microsoft style guide 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.
The most important modes or states are, sorted roughly by importance:
As a general rule, use a push button when the application or dialog window performs an action when the user clicks on it (like Apply, Cancel, Close, 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 (like for example 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.
Also, if you need toggle behaviour (see setToggleButton()) or a button that auto-repeats the activation signal when being pushed down like the arrows in a scrollbar (see setAutoRepeat()), a command button is probably not what you want. In case of 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
Examples: qdir/qdir.cpp xform/xform.cpp layout/layout.cpp drawdemo/drawdemo.cpp popup/popup.cpp qmag/qmag.cpp rot13/rot13.cpp scrollview/scrollview.cpp customlayout/main.cpp
[virtual protected]
Draws the push button, except its label.
See also: drawButtonLabel().
Reimplemented from QButton.
[virtual protected]
Draws the push button label.
See also: drawButton().
Reimplemented from QButton.
[virtual protected]
Handles focus in events for the push button.
Reimplemented from QWidget.
[virtual protected]
Handles focus out events for the push button.
Reimplemented from QWidget.
[virtual protected]
Reimplemented for internal reasons; the API is not affected.
Reimplemented from QWidget.
[slot]
Toggles the state of a toggle button.
See also: isOn(), setOn(), toggled() and isToggleButton().
[virtual protected]
Reimplemented for internal reasons; the API is not affected.
Reimplemented from QWidget.
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 Tech | Trademarks | Qt version 2.1.0 (pre-release)
|