#include <qpushbutton.h>
Inheritance diagram for QPushButton:


The push button, or command button, is perhaps the most commonly used widget in any graphical user interface. Push (click) a button to command the computer to perform some action, or to answer a question. Typical buttons are OK, Apply, Cancel, Close, Yes, No and Help.
A command button is rectangular and typically displays a text label describing its action. A shortcut key can be specified by preceding the preferred character with an ampersand in the text. For example:
QPushButton *button = new QPushButton("&Download", this);
In this example the shortcut is {Alt+D}. See the {QShortcut::mnemonic}{QShortcut} documentation for details (to display an actual ampersand, use '&&').
Push buttons display a textual label, and optionally a small icon. These can be set using the constructors and changed later using setText() and setIcon(). If the button is disabled the appearance of the text and icon will be manipulated with respect to the GUI style to make the button look "disabled".
A push button emits the signal clicked() when it is activated by the mouse, the Spacebar or by a keyboard shortcut. Connect to this signal to perform the button's action. Push buttons also provide less commonly used signals, for example, pressed() and 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 push button that is activated when the user presses the Enter or Return key in a dialog. You can change this with setAutoDefault(). Note that auto-default buttons reserve a little extra space which is necessary to draw a default-button indicator. If you do not want this space around your buttons, call setAutoDefault(false).
Being so central, the button 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.
The most important modes or states are: Available or not (grayed out, disabled). Standard push button, toggling push button or menu button. On or off (only for toggling push buttons). Default or normal. The default button in a dialog can generally be "clicked" using the Enter or Return key. Auto-repeat or not. Pressed down or not.
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 setCheckable()) 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, use a tool button.
A variation of a command button is a menu button. These provide not just one command, but several, since when they are clicked they pop up a menu of options. Use the method setMenu() to associate a popup menu with a push button.
Other classes of buttons are option buttons (see QRadioButton) and check boxes (see QCheckBox).
100% macintosh-pushbutton.png Screenshot of a Macintosh style push button A push button shown in the {Macintosh Style Widget Gallery}{Macintosh widget style}. windowsxp-pushbutton.png Screenshot of a Windows XP style push button A push button shown in the {Windows XP Style Widget Gallery}{Windows XP widget style}. plastique-pushbutton.png Screenshot of a Plastique style push button A push button shown in the {Plastique Style Widget Gallery}{Plastique widget style}.
In Qt, the QAbstractButton base class provides most of the modes and other API, and QPushButton provides GUI logic. See QAbstractButton for more information about the API.
Definition at line 36 of file qpushbutton.h.
Public Slots | |
| void | showMenu () |
Public Member Functions | |
| QPushButton (QWidget *parent=0) | |
| QPushButton (const QString &text, QWidget *parent=0) | |
| QPushButton (const QIcon &icon, const QString &text, QWidget *parent=0) | |
| ~QPushButton () | |
| QSize | sizeHint () const |
| bool | autoDefault () const |
| void | setAutoDefault (bool) |
| bool | isDefault () const |
| void | setDefault (bool) |
| void | setMenu (QMenu *menu) |
| QMenu * | menu () const |
| void | setFlat (bool) |
| bool | isFlat () const |
Protected Member Functions | |
| bool | event (QEvent *e) |
| void | paintEvent (QPaintEvent *) |
| void | keyPressEvent (QKeyEvent *) |
| void | focusInEvent (QFocusEvent *) |
| void | focusOutEvent (QFocusEvent *) |
| QPushButton::QPushButton | ( | QWidget * | parent = 0 |
) | [explicit] |
Constructs a push button with no text and a parent.
Definition at line 227 of file qpushbutton.cpp.
References d.
00228 : QAbstractButton(*new QPushButtonPrivate, parent) 00229 { 00230 Q_D(QPushButton); 00231 d->init(); 00232 }
Constructs a push button with the parent parent and the text text.
Definition at line 239 of file qpushbutton.cpp.
References d, QAbstractButton::setText(), and QAbstractButton::text().
00240 : QAbstractButton(*new QPushButtonPrivate, parent) 00241 { 00242 Q_D(QPushButton); 00243 d->init(); 00244 setText(text); 00245 }
Here is the call graph for this function:

Constructs a push button with an icon and a text, and a parent.
Note that you can also pass a QPixmap object as an icon (thanks to the implicit type conversion provided by C++).
Definition at line 255 of file qpushbutton.cpp.
References d, QAbstractButton::icon(), QAbstractButton::setIcon(), QAbstractButton::setText(), and QAbstractButton::text().
00256 : QAbstractButton(*new QPushButtonPrivate, parent) 00257 { 00258 Q_D(QPushButton); 00259 d->init(); 00260 setText(text); 00261 setIcon(icon); 00262 }
Here is the call graph for this function:

| QPushButton::~QPushButton | ( | ) |
| QSize QPushButton::sizeHint | ( | ) | const [virtual] |
Reimplemented from QWidget.
Definition at line 362 of file qpushbutton.cpp.
References QStyle::CT_PushButton, d, QWidget::ensurePolished(), QWidget::fontMetrics(), QString::fromLatin1(), QApplication::globalStrut(), h, QSize::height(), QAbstractButton::icon(), QStyleOptionButton::iconSize, isNull(), menu(), QStyle::pixelMetric(), QStyle::PM_MenuButtonIndicator, qMax(), s, QFontMetrics::size(), QWidget::style(), QAbstractButton::text(), Qt::TextShowMnemonic, w, and QSize::width().
Referenced by QProgressDialogPrivate::layout(), Q3ProgressDialog::layout(), Q3TabDialog::setSizes(), Q3ProgressDialog::sizeHint(), and Q3FileDialog::updateGeometries().
00363 { 00364 Q_D(const QPushButton); 00365 ensurePolished(); 00366 00367 int w = 0, h = 0; 00368 00369 QStyleOptionButton opt = d->getStyleOption(); 00370 00371 // calculate contents size... 00372 #ifndef QT_NO_ICON 00373 if (!icon().isNull()) { 00374 int ih = opt.iconSize.height(); 00375 int iw = opt.iconSize.width() + 4; 00376 w += iw; 00377 h = qMax(h, ih); 00378 } 00379 #endif 00380 #ifndef QT_NO_MENU 00381 if (menu()) 00382 w += style()->pixelMetric(QStyle::PM_MenuButtonIndicator, &opt, this); 00383 #endif 00384 QString s(text()); 00385 bool empty = s.isEmpty(); 00386 if (empty) 00387 s = QString::fromLatin1("XXXX"); 00388 QFontMetrics fm = fontMetrics(); 00389 QSize sz = fm.size(Qt::TextShowMnemonic, s); 00390 if(!empty || !w) 00391 w += sz.width(); 00392 if(!empty || !h) 00393 h = qMax(h, sz.height()); 00394 return (style()->sizeFromContents(QStyle::CT_PushButton, &opt, QSize(w, h), this). 00395 expandedTo(QApplication::globalStrut())); 00396 }
Here is the call graph for this function:

| bool QPushButton::autoDefault | ( | ) | const |
Definition at line 329 of file qpushbutton.cpp.
References QPushButtonPrivate::Auto, and d.
Referenced by focusInEvent(), focusOutEvent(), keyPressEvent(), and QDialog::setVisible().
00330 { 00331 Q_D(const QPushButton); 00332 if(d->autoDefault == QPushButtonPrivate::Auto) 00333 return ( d->dialogParent() != 0 ); 00334 return d->autoDefault; 00335 }
| void QPushButton::setAutoDefault | ( | bool | ) |
Definition at line 318 of file qpushbutton.cpp.
References QPushButtonPrivate::Auto, d, QPushButtonPrivate::Off, QPushButtonPrivate::On, QWidget::update(), and QWidget::updateGeometry().
Referenced by qdesigner_internal::QtGradientDialog::QtGradientDialog(), and Server::Server().
00319 { 00320 Q_D(QPushButton); 00321 uint state = enable ? QPushButtonPrivate::On : QPushButtonPrivate::Off; 00322 if (d->autoDefault != QPushButtonPrivate::Auto && d->autoDefault == state) 00323 return; 00324 d->autoDefault = state; 00325 update(); 00326 updateGeometry(); 00327 }
Here is the call graph for this function:

| bool QPushButton::isDefault | ( | ) | const |
Definition at line 353 of file qpushbutton.cpp.
References d.
Referenced by QAccessibleButton::state(), and QAccessibleButton::text().
00354 { 00355 Q_D(const QPushButton); 00356 return d->defaultButton; 00357 }
| void QPushButton::setDefault | ( | bool | ) |
Definition at line 337 of file qpushbutton.cpp.
References d, QAccessible::StateChanged, QWidget::update(), and QAccessible::updateAccessibility().
Referenced by BlockingClient::BlockingClient(), Client::Client(), Window::createMessageGroupBox(), WidgetGallery::createTopRightGroupBox(), FtpWindow::ftpCommandFinished(), FtpWindow::FtpWindow(), QDialogPrivate::hideDefault(), HttpWindow::HttpWindow(), QColorDialogPrivate::init(), QInputDialogPrivate::init(), Q3FileDialog::init(), ItemDialog::ItemDialog(), qdesigner_internal::OldSignalSlotDialog::OldSignalSlotDialog(), NewForm::on_treeWidget_currentItemChanged(), PluginDialog::PluginDialog(), Q3TabDialog::Q3TabDialog(), Q3Wizard::Q3Wizard(), QFontDialog::QFontDialog(), qdesigner_internal::RichTextEditorDialog::RichTextEditorDialog(), SpreadSheet::runInputDialog(), QDialogPrivate::setDefault(), QDialog::setVisible(), Oubliette::showInventory(), TableEditor::TableEditor(), and VersionDialog::VersionDialog().
00338 { 00339 Q_D(QPushButton); 00340 if (d->defaultButton == enable) 00341 return; 00342 d->defaultButton = enable; 00343 if (d->defaultButton) { 00344 if (QDialog *dlg = d->dialogParent()) 00345 dlg->d_func()->setMainDefault(this); 00346 } 00347 update(); 00348 #ifndef QT_NO_ACCESSIBILITY 00349 QAccessible::updateAccessibility(this, 0, QAccessible::StateChanged); 00350 #endif 00351 }
Here is the call graph for this function:

| void QPushButton::setMenu | ( | QMenu * | menu | ) |
Associates the popup menu menu with this push button. This turns the button into a menu button, which in some styles will produce a small triangle to the right of the button's text.
Ownership of the menu is not transferred to the push button.
Definition at line 473 of file qpushbutton.cpp.
References QWidget::addAction(), QObject::connect(), d, QObject::disconnect(), menu(), QAbstractButton::pressed(), QWidget::removeAction(), SIGNAL, SLOT, QWidget::update(), and QWidget::updateGeometry().
Referenced by Window::createPushButtonGroup().
00474 { 00475 Q_D(QPushButton); 00476 if (menu == d->menu) 00477 return; 00478 00479 if (menu && !d->menu) { 00480 disconnect(this, SIGNAL(pressed()), this, SLOT(_q_popupPressed())); 00481 connect(this, SIGNAL(pressed()), this, SLOT(_q_popupPressed())); 00482 } 00483 if (d->menu) 00484 removeAction(d->menu->menuAction()); 00485 d->menu = menu; 00486 if (d->menu) 00487 addAction(d->menu->menuAction()); 00488 update(); 00489 updateGeometry(); 00490 }
Here is the call graph for this function:

| QMenu * QPushButton::menu | ( | ) | const |
Returns the button's associated popup menu or 0 if no popup menu has been set.
Definition at line 498 of file qpushbutton.cpp.
References d.
Referenced by AccessibleFactory::create(), QAccessibleButton::doAction(), setMenu(), sizeHint(), and QAccessibleButton::state().
00499 { 00500 Q_D(const QPushButton); 00501 return d->menu; 00502 }
| void QPushButton::setFlat | ( | bool | ) |
Definition at line 563 of file qpushbutton.cpp.
References d, QWidget::update(), and QWidget::updateGeometry().
Referenced by Window::createPushButtonGroup(), and WidgetGallery::createTopRightGroupBox().
00564 { 00565 Q_D(QPushButton); 00566 if (d->flat == flat) 00567 return; 00568 d->flat = flat; 00569 update(); 00570 updateGeometry(); 00571 }
Here is the call graph for this function:

| bool QPushButton::isFlat | ( | ) | const |
Definition at line 573 of file qpushbutton.cpp.
References d.
00574 { 00575 Q_D(const QPushButton); 00576 return d->flat; 00577 }
| void QPushButton::showMenu | ( | ) | [slot] |
Shows (pops up) the associated popup menu. If there is no such menu, this function does nothing. This function does not return until the popup menu has been closed by the user.
Definition at line 509 of file qpushbutton.cpp.
References d, and QAbstractButton::setDown().
Referenced by QAccessibleButton::doAction().
00510 { 00511 Q_D(QPushButton); 00512 if (!d || !d->menu) 00513 return; 00514 setDown(true); 00515 d->_q_popupPressed(); 00516 }
| bool QPushButton::event | ( | QEvent * | e | ) | [protected, virtual] |
Reimplemented from QAbstractButton.
Definition at line 580 of file qpushbutton.cpp.
References QAbstractButton::event().
Referenced by qdesigner_internal::StyledButton::paintEvent().
00581 { 00582 return QAbstractButton::event(e); 00583 }
Here is the call graph for this function:

| void QPushButton::paintEvent | ( | QPaintEvent * | ) | [protected, virtual] |
Implements QAbstractButton.
Definition at line 402 of file qpushbutton.cpp.
References QStyle::CE_PushButton, d, and p.
Referenced by qdesigner_internal::StyledButton::paintEvent().
00403 { 00404 Q_D(QPushButton); 00405 QStylePainter p(this); 00406 p.drawControl(QStyle::CE_PushButton, d->getStyleOption()); 00407 }
| void QPushButton::keyPressEvent | ( | QKeyEvent * | e | ) | [protected, virtual] |
Reimplemented from QAbstractButton.
Definition at line 411 of file qpushbutton.cpp.
References autoDefault(), QAbstractButton::click(), d, QKeyEvent::key(), Qt::Key_Enter, Qt::Key_Return, and QAbstractButton::keyPressEvent().
00412 { 00413 Q_D(QPushButton); 00414 switch (e->key()) { 00415 case Qt::Key_Enter: 00416 case Qt::Key_Return: 00417 if (autoDefault() || d->defaultButton) { 00418 click(); 00419 break; 00420 } 00421 // fall through 00422 default: 00423 QAbstractButton::keyPressEvent(e); 00424 } 00425 }
Here is the call graph for this function:

| void QPushButton::focusInEvent | ( | QFocusEvent * | e | ) | [protected, virtual] |
Reimplemented from QAbstractButton.
Definition at line 430 of file qpushbutton.cpp.
References autoDefault(), d, QAbstractButton::focusInEvent(), Qt::PopupFocusReason, QFocusEvent::reason(), and QWidget::window().
00431 { 00432 Q_D(QPushButton); 00433 if (e->reason() != Qt::PopupFocusReason && autoDefault() && !d->defaultButton) { 00434 d->defaultButton = true; 00435 QDialog *dlg = qobject_cast<QDialog*>(window()); 00436 if (dlg) 00437 dlg->d_func()->setDefault(this); 00438 } 00439 QAbstractButton::focusInEvent(e); 00440 }
Here is the call graph for this function:

| void QPushButton::focusOutEvent | ( | QFocusEvent * | e | ) | [protected, virtual] |
Reimplemented from QAbstractButton.
Definition at line 445 of file qpushbutton.cpp.
References autoDefault(), d, QAbstractButton::focusOutEvent(), Qt::PopupFocusReason, QFocusEvent::reason(), QAbstractButton::setDown(), and QWidget::window().
00446 { 00447 Q_D(QPushButton); 00448 if (e->reason() != Qt::PopupFocusReason && autoDefault() && d->defaultButton) { 00449 QDialog *dlg = qobject_cast<QDialog*>(window()); 00450 if (dlg) 00451 dlg->d_func()->setDefault(0); 00452 else 00453 d->defaultButton = false; 00454 } 00455 00456 QAbstractButton::focusOutEvent(e); 00457 #ifndef QT_NO_MENU 00458 if (d->menu && d->menu->isVisible()) // restore pressed status 00459 setDown(true); 00460 #endif 00461 }
Here is the call graph for this function:

1.5.1