QToolButton Class Reference

#include <qtoolbutton.h>

Inheritance diagram for QToolButton:

Inheritance graph
[legend]
Collaboration diagram for QToolButton:

Collaboration graph
[legend]
List of all members.

Detailed Description

The QToolButton class provides a quick-access button to commands or options, usually used inside a QToolBar.

A tool button is a special button that provides quick-access to specific commands or options. As opposed to a normal command button, a tool button usually doesn't show a text label, but shows an icon instead.

Tool buttons are normally created when new QAction instances are created with QToolBar::addAction() or existing actions are added to a toolbar with QToolBar::addAction(). It is also possible to construct tool buttons in the same way as any other widget, and arrange them alongside other widgets in layouts.

One classic use of a tool button is to select tools; for example, the "pen" tool in a drawing program. This would be implemented by using a QToolButton as a toggle button (see setToggleButton()).

QToolButton supports auto-raising. In auto-raise mode, the button draws a 3D frame only when the mouse points at it. The feature is automatically turned on when a button is used inside a QToolBar. Change it with setAutoRaise().

A tool button's icon is set as QIcon. This makes it possible to specify different pixmaps for the disabled and active state. The disabled pixmap is used when the button's functionality is not available. The active pixmap is displayed when the button is auto-raised because the mouse pointer is hovering over it.

The button's look and dimension is adjustable with setToolButtonStyle() and setIconSize(). When used inside a QToolBar in a QMainWindow, the button automatically adjusts to QMainWindow's settings (see QMainWindow::setToolButtonStyle() and QMainWindow::setIconSize()). Instead of an icon, a tool button can also display an arrow symbol, specified with arrowType.

A tool button can offer additional choices in a popup menu. The popup menu can be set using setMenu(). Use setPopupMode() to configure the different modes available for tool buttons with a menu set. The default mode is DelayedPopupMode which is sometimes used with the "Back" button in a web browser. After pressing and holding the button down for a while, a menu pops up showing a list of possible pages to jump to. The default delay is 600 ms; you can adjust it with setPopupDelay().

100% assistant-toolbar1.png Qt Assistant's toolbar with tool buttons Qt Assistant's toolbar contains tool buttons that are associated with actions used in other parts of the main window.

See also:
QPushButton, QToolBar, QMainWindow, QAction, {fowler}{GUI Design Handbook: Push Button}

Definition at line 38 of file qtoolbutton.h.

Public Types

enum  ToolButtonPopupMode

Public Slots

void showMenu ()
void setToolButtonStyle (Qt::ToolButtonStyle style)
void setDefaultAction (QAction *)

Signals

void triggered (QAction *)

Public Member Functions

 QToolButton (QWidget *parent=0)
 ~QToolButton ()
QSize sizeHint () const
QSize minimumSizeHint () const
Qt::ToolButtonStyle toolButtonStyle () const
Qt::ArrowType arrowType () const
void setArrowType (Qt::ArrowType type)
void setMenu (QMenu *menu)
QMenumenu () const
void setPopupMode (ToolButtonPopupMode mode)
ToolButtonPopupMode popupMode () const
QActiondefaultAction () const
void setAutoRaise (bool enable)
bool autoRaise () const

Protected Member Functions

 QToolButton (QToolButtonPrivate &, QWidget *parent)
bool event (QEvent *e)
void mousePressEvent (QMouseEvent *)
void mouseReleaseEvent (QMouseEvent *)
void paintEvent (QPaintEvent *)
void actionEvent (QActionEvent *)
void enterEvent (QEvent *)
void leaveEvent (QEvent *)
void timerEvent (QTimerEvent *)
void changeEvent (QEvent *)
bool hitButton (const QPoint &pos) const
void nextCheckState ()

Private Member Functions

 Q_PRIVATE_SLOT (d_func(), void _q_buttonPressed()) Q_PRIVATE_SLOT(d_func()


Member Enumeration Documentation

enum QToolButton::ToolButtonPopupMode

Describes how a menu should be popped up for tool buttons that has a menu set or contains a list of actions.

DelayedPopup After pressing and holding the tool button down for a certain amount of time (the timeout is style dependant, see QStyle::SH_ToolButton_PopupDelay), the menu is displayed. A typical application example is the "back" button in some web browsers's tool bars. If the user clicks it, the browser simply browses back to the previous page. If the user presses and holds the button down for a while, the tool button shows a menu containing the current history list

MenuButtonPopup In this mode the tool button displays a special arrow to indicate that a menu is present. The menu is displayed when the arrow part of the button is pressed.

InstantPopup The menu is displayed, without delay, when the tool button is pressed. In this mode, the button's own action is not triggered.

Definition at line 50 of file qtoolbutton.h.

00050                              {
00051         DelayedPopup,
00052         MenuButtonPopup,
00053         InstantPopup
00054     };


Constructor & Destructor Documentation

QToolButton::QToolButton ( QWidget parent = 0  )  [explicit]

Constructs an empty tool button with parent parent.

Definition at line 158 of file qtoolbutton.cpp.

References d.

00159     : QAbstractButton(*new QToolButtonPrivate, parent)
00160 {
00161     Q_D(QToolButton);
00162     d->init();
00163 }

QToolButton::~QToolButton (  ) 

Destroys the object and frees any allocated resources.

Definition at line 345 of file qtoolbutton.cpp.

00346 {
00347 }

QToolButton::QToolButton ( QToolButtonPrivate ,
QWidget parent 
) [protected]

Definition at line 1016 of file qtoolbutton.cpp.

References d.

01017     :QAbstractButton(dd, parent)
01018 {
01019     Q_D(QToolButton);
01020     d->init();
01021 }


Member Function Documentation

QSize QToolButton::sizeHint (  )  const [virtual]

Reimplemented from QWidget.

Definition at line 352 of file qtoolbutton.cpp.

References QStyle::CT_ToolButton, d, QWidget::ensurePolished(), QWidget::fontMetrics(), QApplication::globalStrut(), h, QSize::height(), QAbstractButton::icon(), QStyleOptionToolButton::iconSize, MenuButtonPopup, QStyle::pixelMetric(), QStyle::PM_MenuButtonIndicator, QStyleOption::rect, QRect::setHeight(), QSize::setWidth(), QFontMetrics::size(), QStyle::sizeFromContents(), QWidget::style(), QAbstractButton::text(), Qt::TextShowMnemonic, Qt::ToolButtonIconOnly, QStyleOptionToolButton::toolButtonStyle, Qt::ToolButtonTextBesideIcon, Qt::ToolButtonTextOnly, Qt::ToolButtonTextUnderIcon, w, QSize::width(), and QFontMetrics::width().

Referenced by minimumSizeHint(), Button::sizeHint(), and SortingBox::updateButtonGeometry().

00353 {
00354     Q_D(const QToolButton);
00355     ensurePolished();
00356 
00357     int w = 0, h = 0;
00358     QStyleOptionToolButton opt = d->getStyleOption();
00359 
00360     QFontMetrics fm = fontMetrics();
00361     if (opt.toolButtonStyle != Qt::ToolButtonTextOnly) {
00362         QSize icon = opt.iconSize;
00363         w = icon.width();
00364         h = icon.height();
00365     }
00366 
00367     if (opt.toolButtonStyle != Qt::ToolButtonIconOnly) {
00368         QSize textSize = fm.size(Qt::TextShowMnemonic, text());
00369         textSize.setWidth(textSize.width() + fm.width(QLatin1Char(' '))*2);
00370         if (opt.toolButtonStyle == Qt::ToolButtonTextUnderIcon) {
00371             h += 4 + textSize.height();
00372             if (textSize.width() > w)
00373                 w = textSize.width();
00374         } else if (opt.toolButtonStyle == Qt::ToolButtonTextBesideIcon) {
00375             w += 4 + textSize.width();
00376             if (textSize.height() > h)
00377                 h = textSize.height();
00378         } else { // TextOnly
00379             w = textSize.width();
00380             h = textSize.height();
00381         }
00382     }
00383 
00384     opt.rect.setHeight(h); // PM_MenuButtonIndicator depends on the height
00385     if (d->popupMode == MenuButtonPopup)
00386         w += style()->pixelMetric(QStyle::PM_MenuButtonIndicator, &opt, this);
00387 
00388     return style()->sizeFromContents(QStyle::CT_ToolButton, &opt, QSize(w, h), this).
00389             expandedTo(QApplication::globalStrut());
00390 }

Here is the call graph for this function:

QSize QToolButton::minimumSizeHint (  )  const [virtual]

Reimplemented from QWidget.

Definition at line 395 of file qtoolbutton.cpp.

References sizeHint().

00396 {
00397     return sizeHint();
00398 }

Here is the call graph for this function:

Qt::ToolButtonStyle QToolButton::toolButtonStyle (  )  const

Definition at line 431 of file qtoolbutton.cpp.

References d.

00432 {
00433     Q_D(const QToolButton);
00434     return d->toolButtonStyle;
00435 }

Qt::ArrowType QToolButton::arrowType (  )  const

Definition at line 437 of file qtoolbutton.cpp.

References d.

00438 {
00439     Q_D(const QToolButton);
00440     return d->arrowType;
00441 }

void QToolButton::setArrowType ( Qt::ArrowType  type  ) 

Definition at line 457 of file qtoolbutton.cpp.

References d, QWidget::isVisible(), QWidget::update(), and QWidget::updateGeometry().

Referenced by QTabBarPrivate::layoutTabs(), QDesignerQ3WidgetStack::QDesignerQ3WidgetStack(), and QDesignerStackedWidget::QDesignerStackedWidget().

00458 {
00459     Q_D(QToolButton);
00460     if (d->arrowType == type)
00461         return;
00462 
00463     d->arrowType = type;
00464     updateGeometry();
00465     if (isVisible()) {
00466         update();
00467     }
00468 }

Here is the call graph for this function:

void QToolButton::setMenu ( QMenu menu  ) 

Associates the given menu with this tool button.

The menu will be shown according to the button's popupMode.

Ownership of the menu is not transferred to the tool button.

See also:
menu()

Definition at line 704 of file qtoolbutton.cpp.

References QWidget::addAction(), d, menu(), QMenu::menuAction(), QWidget::removeAction(), and QWidget::update().

Referenced by QCalendarWidgetPrivate::createHeader(), and QMenuBarPrivate::updateGeometries().

00705 {
00706     Q_D(QToolButton);
00707 
00708     if (d->menuAction)
00709         removeAction(d->menuAction);
00710 
00711     if (menu) {
00712         d->menuAction = menu->menuAction();
00713         addAction(d->menuAction);
00714     } else {
00715         d->menuAction = 0;
00716     }
00717     update();
00718 }

Here is the call graph for this function:

QMenu * QToolButton::menu (  )  const

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

See also:
setMenu()

Definition at line 725 of file qtoolbutton.cpp.

References d.

Referenced by QAccessibleToolButton::actionCount(), QAccessibleToolButton::isSplitButton(), QCalToolButton::paintEvent(), setMenu(), and QMenuBarPrivate::updateGeometries().

00726 {
00727     Q_D(const QToolButton);
00728     if (d->menuAction)
00729         return d->menuAction->menu();
00730     return 0;
00731 }

void QToolButton::setPopupMode ( ToolButtonPopupMode  mode  ) 

Definition at line 903 of file qtoolbutton.cpp.

References d.

Referenced by QCalendarWidgetPrivate::createHeader(), Q3ToolBar::createPopup(), QMenuBarExtension::QMenuBarExtension(), QToolBarExtension::QToolBarExtension(), and setDefaultAction().

00904 {
00905     Q_D(QToolButton);
00906     d->popupMode = mode;
00907 }

QToolButton::ToolButtonPopupMode QToolButton::popupMode (  )  const

Definition at line 909 of file qtoolbutton.cpp.

References d.

Referenced by QAccessibleToolButton::isSplitButton().

00910 {
00911     Q_D(const QToolButton);
00912     return d->popupMode;
00913 }

QAction * QToolButton::defaultAction (  )  const

Returns the default action.

See also:
setDefaultAction()

Definition at line 988 of file qtoolbutton.cpp.

References d.

00989 {
00990     Q_D(const QToolButton);
00991     return d->defaultAction;
00992 }

void QToolButton::setAutoRaise ( bool  enable  ) 

Definition at line 924 of file qtoolbutton.cpp.

References d, and QWidget::update().

Referenced by QCalendarWidgetPrivate::createHeader(), QToolBarPrivate::createItem(), Q3FileDialog::init(), TabbedBrowser::init(), Q3ToolBarExtensionWidget::Q3ToolBarExtensionWidget(), QDesignerQ3WidgetStack::QDesignerQ3WidgetStack(), QDesignerStackedWidget::QDesignerStackedWidget(), QMenuBarExtension::QMenuBarExtension(), QToolBarExtension::QToolBarExtension(), and QFileDialogPrivate::setupToolButtons().

00925 {
00926     Q_D(QToolButton);
00927     d->autoRaise = enable;
00928 
00929     update();
00930 }

bool QToolButton::autoRaise (  )  const

Definition at line 932 of file qtoolbutton.cpp.

References d.

00933 {
00934     Q_D(const QToolButton);
00935     return d->autoRaise;
00936 }

void QToolButton::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 738 of file qtoolbutton.cpp.

References d, and QWidget::repaint().

Referenced by QAccessibleToolButton::doAction(), and mousePressEvent().

00739 {
00740     Q_D(QToolButton);
00741     if (!d->hasMenu()) {
00742         d->menuButtonDown = false;
00743         return; // no menu to show
00744     }
00745 
00746     d->menuButtonDown = true;
00747     repaint();
00748     d->popupTimer.stop();
00749     d->popupTimerDone();
00750 }

void QToolButton::setToolButtonStyle ( Qt::ToolButtonStyle  style  )  [slot]

Definition at line 444 of file qtoolbutton.cpp.

References d, QWidget::isVisible(), QWidget::update(), and QWidget::updateGeometry().

Referenced by QToolBarPrivate::createItem(), EditorWithReset::EditorWithReset(), qdesigner_internal::RoleEditor::RoleEditor(), and qdesigner_internal::Sentinel::Sentinel().

00445 {
00446     Q_D(QToolButton);
00447     if (d->toolButtonStyle == style)
00448         return;
00449 
00450     d->toolButtonStyle = style;
00451     updateGeometry();
00452     if (isVisible()) {
00453         update();
00454     }
00455 }

void QToolButton::setDefaultAction ( QAction action  )  [slot]

Sets the default action to action.

If a tool button has a default action, the action defines the button's properties like text, icon, tool tip, etc.

Definition at line 944 of file qtoolbutton.cpp.

References QWidget::actions(), QWidget::addAction(), d, MenuButtonPopup, QAbstractButton::setCheckable(), QAbstractButton::setChecked(), QWidget::setEnabled(), QWidget::setFont(), QAbstractButton::setIcon(), setPopupMode(), QWidget::setStatusTip(), QAbstractButton::setText(), QWidget::setToolTip(), and QWidget::setWhatsThis().

Referenced by Q3ToolBar::actionEvent(), actionEvent(), and QToolBarPrivate::createItem().

00945 {
00946     Q_D(QToolButton);
00947 #ifndef QT_NO_MENU
00948     bool hadMenu = false;
00949     hadMenu = d->hasMenu();
00950 #endif
00951     d->defaultAction = action;
00952     if (!action)
00953         return;
00954     if (!actions().contains(action))
00955         addAction(action);
00956     setText(action->iconText());
00957     setIcon(action->icon());
00958 #ifndef QT_NO_TOOLTIP
00959     setToolTip(action->toolTip());
00960 #endif
00961 #ifndef QT_NO_STATUSTIP
00962     setStatusTip(action->statusTip());
00963 #endif
00964 #ifndef QT_NO_WHATSTHIS
00965     setWhatsThis(action->whatsThis());
00966 #endif
00967 #ifndef QT_NO_MENU
00968     if (action->menu() && !hadMenu) {
00969         // new 'default' popup mode defined introduced by tool bar. We
00970         // should have changed QToolButton's default instead. Do that
00971         // in 4.2.
00972         setPopupMode(QToolButton::MenuButtonPopup);
00973     }
00974 #endif
00975     setCheckable(action->isCheckable());
00976     setChecked(action->isChecked());
00977     setEnabled(action->isEnabled());
00978     if (action->d_func()->fontSet)
00979         setFont(action->font());
00980 }

void QToolButton::triggered ( QAction action  )  [signal]

This signal is emitted when the given action is triggered.

The action may also be associated with other parts of the user interface, such as menu items and keyboard shortcuts. Sharing actions in this way helps make the user interface more consistent and is often less work to implement.

Referenced by actionEvent().

bool QToolButton::event ( QEvent e  )  [protected, virtual]

Reimplemented from QAbstractButton.

Definition at line 1009 of file qtoolbutton.cpp.

References QAbstractButton::event().

Referenced by actionEvent().

01010 {
01011     return QAbstractButton::event(e);
01012 }

Here is the call graph for this function:

void QToolButton::mousePressEvent ( QMouseEvent e  )  [protected, virtual]

Reimplemented from QAbstractButton.

Definition at line 579 of file qtoolbutton.cpp.

References QMouseEvent::button(), QStyle::CC_ToolButton, d, Qt::LeftButton, MenuButtonPopup, QToolButtonPrivate::MenuButtonPressed, QAbstractButton::mousePressEvent(), QMouseEvent::pos(), QStyle::SC_ToolButtonMenu, showMenu(), QWidget::style(), QStyle::subControlRect(), and QToolButtonPrivate::ToolButtonPressed.

Referenced by QCalToolButton::mousePressEvent().

00580 {
00581     Q_D(QToolButton);
00582 #ifndef QT_NO_MENU
00583     QStyleOptionToolButton opt = d->getStyleOption();
00584     if (e->button() == Qt::LeftButton && d->popupMode == MenuButtonPopup) {
00585         QRect popupr = style()->subControlRect(QStyle::CC_ToolButton, &opt,
00586                                                QStyle::SC_ToolButtonMenu, this);
00587         if (popupr.isValid() && popupr.contains(e->pos())) {
00588             d->buttonPressed = QToolButtonPrivate::MenuButtonPressed;
00589             showMenu();
00590             return;
00591         }
00592     }
00593 #endif
00594     d->buttonPressed = QToolButtonPrivate::ToolButtonPressed;
00595     QAbstractButton::mousePressEvent(e);
00596 }

Here is the call graph for this function:

void QToolButton::mouseReleaseEvent ( QMouseEvent e  )  [protected, virtual]

Reimplemented from QAbstractButton.

Definition at line 601 of file qtoolbutton.cpp.

References d, QAbstractButton::mouseReleaseEvent(), and QToolButtonPrivate::NoButtonPressed.

Referenced by QCalToolButton::mouseReleaseEvent().

00602 {
00603     Q_D(QToolButton);
00604     QAbstractButton::mouseReleaseEvent(e);
00605     d->buttonPressed = QToolButtonPrivate::NoButtonPressed;
00606 }

Here is the call graph for this function:

void QToolButton::paintEvent ( QPaintEvent event  )  [protected, virtual]

Paints the button in response to the paint event.

Implements QAbstractButton.

Definition at line 475 of file qtoolbutton.cpp.

References QStyle::CC_ToolButton, d, and p.

Referenced by qdesigner_internal::QtBrushButton::paintEvent(), qdesigner_internal::QtColorButton::paintEvent(), and QCalToolButton::paintEvent().

00476 {
00477     Q_D(QToolButton);
00478     QStylePainter p(this);
00479     p.drawComplexControl(QStyle::CC_ToolButton, d->getStyleOption());
00480 }

void QToolButton::actionEvent ( QActionEvent event  )  [protected, virtual]

Reimplemented from QWidget.

Definition at line 485 of file qtoolbutton.cpp.

References QEvent::ActionAdded, QEvent::ActionChanged, QWidget::actionEvent(), QEvent::ActionRemoved, QObject::connect(), d, QObject::disconnect(), event(), setDefaultAction(), SIGNAL, SLOT, and triggered().

00486 {
00487     Q_D(QToolButton);
00488     QAction *action = event->action();
00489     switch (event->type()) {
00490     case QEvent::ActionChanged:
00491         if (action == d->defaultAction)
00492             setDefaultAction(action); // update button state
00493         break;
00494     case QEvent::ActionAdded:
00495         connect(action, SIGNAL(triggered()), this, SLOT(_q_actionTriggered()));
00496         break;
00497     case QEvent::ActionRemoved:
00498         if (d->defaultAction == action)
00499             d->defaultAction = 0;
00500 #ifndef QT_NO_MENU
00501         if (action == d->menuAction)
00502             d->menuAction = 0;
00503 #endif
00504         action->disconnect(this);
00505         break;
00506     default:
00507         ;
00508     }
00509     QAbstractButton::actionEvent(event);
00510 }

Here is the call graph for this function:

void QToolButton::enterEvent ( QEvent e  )  [protected, virtual]

Reimplemented from QWidget.

Definition at line 522 of file qtoolbutton.cpp.

References d, QWidget::enterEvent(), and QWidget::update().

Referenced by QCalToolButton::enterEvent().

00523 {
00524     Q_D(QToolButton);
00525     if (d->autoRaise)
00526         update();
00527 
00528     QAbstractButton::enterEvent(e);
00529 }

Here is the call graph for this function:

void QToolButton::leaveEvent ( QEvent e  )  [protected, virtual]

Reimplemented from QWidget.

Definition at line 535 of file qtoolbutton.cpp.

References d, QWidget::leaveEvent(), and QWidget::update().

Referenced by QCalToolButton::leaveEvent().

00536 {
00537     Q_D(QToolButton);
00538     if (d->autoRaise)
00539         update();
00540 
00541     QAbstractButton::leaveEvent(e);
00542 }

Here is the call graph for this function:

void QToolButton::timerEvent ( QTimerEvent e  )  [protected, virtual]

Reimplemented from QAbstractButton.

Definition at line 548 of file qtoolbutton.cpp.

References d, QAbstractButton::timerEvent(), and QTimerEvent::timerId().

00549 {
00550 #ifndef QT_NO_MENU
00551     Q_D(QToolButton);
00552     if (e->timerId() == d->popupTimer.timerId()) {
00553         d->popupTimerDone();
00554         return;
00555     }
00556 #endif
00557     QAbstractButton::timerEvent(e);
00558 }

Here is the call graph for this function:

void QToolButton::changeEvent ( QEvent e  )  [protected, virtual]

Reimplemented from QAbstractButton.

Definition at line 564 of file qtoolbutton.cpp.

References QAbstractButton::changeEvent(), d, QEvent::ParentChange, QWidget::parentWidget(), and QEvent::type().

00565 {
00566 #ifndef QT_NO_TOOLBAR
00567     Q_D(QToolButton);
00568     if (e->type() == QEvent::ParentChange) {
00569         if (qobject_cast<QToolBar*>(parentWidget()))
00570             d->autoRaise = true;
00571     }
00572 #endif
00573     QAbstractButton::changeEvent(e);
00574 }

Here is the call graph for this function:

bool QToolButton::hitButton ( const QPoint pos  )  const [protected, virtual]

Reimplemented from QAbstractButton.

Definition at line 611 of file qtoolbutton.cpp.

References d, QAbstractButton::hitButton(), QToolButtonPrivate::MenuButtonPressed, and QWidget::pos().

00612 {
00613     Q_D(const QToolButton);
00614     if(QAbstractButton::hitButton(pos))
00615         return (d->buttonPressed != QToolButtonPrivate::MenuButtonPressed);
00616     return false;
00617 }

Here is the call graph for this function:

void QToolButton::nextCheckState (  )  [protected, virtual]

Reimplemented from QAbstractButton.

Definition at line 999 of file qtoolbutton.cpp.

References d, and QAbstractButton::nextCheckState().

01000 {
01001     Q_D(QToolButton);
01002     if (!d->defaultAction)
01003         QAbstractButton::nextCheckState();
01004     else
01005         d->defaultAction->trigger();
01006 }

Here is the call graph for this function:

QToolButton::Q_PRIVATE_SLOT ( d_func()  ,
void   _q_buttonPressed() 
) [private]

Reimplemented from QWidget.


The documentation for this class was generated from the following files:
Generated on Thu Mar 15 19:35:52 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1