📄 qtoolbutton.cpp
字号:
int w = 0, h = 0; QStyleOptionToolButton opt; initStyleOption(&opt); QFontMetrics fm = fontMetrics(); if (opt.toolButtonStyle != Qt::ToolButtonTextOnly) { QSize icon = opt.iconSize; w = icon.width(); h = icon.height(); } if (opt.toolButtonStyle != Qt::ToolButtonIconOnly) { QSize textSize = fm.size(Qt::TextShowMnemonic, text()); textSize.setWidth(textSize.width() + fm.width(QLatin1Char(' '))*2); if (opt.toolButtonStyle == Qt::ToolButtonTextUnderIcon) { h += 4 + textSize.height(); if (textSize.width() > w) w = textSize.width(); } else if (opt.toolButtonStyle == Qt::ToolButtonTextBesideIcon) { w += 4 + textSize.width(); if (textSize.height() > h) h = textSize.height(); } else { // TextOnly w = textSize.width(); h = textSize.height(); } } opt.rect.setHeight(h); // PM_MenuButtonIndicator depends on the height if (d->popupMode == MenuButtonPopup) w += style()->pixelMetric(QStyle::PM_MenuButtonIndicator, &opt, this); d->sizeHint = style()->sizeFromContents(QStyle::CT_ToolButton, &opt, QSize(w, h), this). expandedTo(QApplication::globalStrut()); return d->sizeHint;}/*! \reimp */QSize QToolButton::minimumSizeHint() const{ return sizeHint();}/*! \enum QToolButton::TextPosition \compat This enum describes the position of the tool button's text label in relation to the tool button's icon. \value BesideIcon The text appears beside the icon. \value BelowIcon The text appears below the icon. \omitvalue Right \omitvalue Under*//*! \property QToolButton::toolButtonStyle \brief whether the tool button displays an icon only, text only, or text beside/below the icon. The default is Qt::ToolButtonIconOnly. QToolButton automatically connects this slot to the relevant signal in the QMainWindow in which is resides.*//*! \property QToolButton::arrowType \brief whether the button displays an arrow instead of a normal icon This displays an arrow as the icon for the QToolButton.*/Qt::ToolButtonStyle QToolButton::toolButtonStyle() const{ Q_D(const QToolButton); return d->toolButtonStyle;}Qt::ArrowType QToolButton::arrowType() const{ Q_D(const QToolButton); return d->arrowType;}void QToolButton::setToolButtonStyle(Qt::ToolButtonStyle style){ Q_D(QToolButton); if (d->toolButtonStyle == style) return; d->toolButtonStyle = style; d->sizeHint = QSize(); updateGeometry(); if (isVisible()) { update(); }}void QToolButton::setArrowType(Qt::ArrowType type){ Q_D(QToolButton); if (d->arrowType == type) return; d->arrowType = type; d->sizeHint = QSize(); updateGeometry(); if (isVisible()) { update(); }}/*! \fn void QToolButton::paintEvent(QPaintEvent *event) Paints the button in response to the paint \a event.*/void QToolButton::paintEvent(QPaintEvent *){ QStylePainter p(this); QStyleOptionToolButton opt; initStyleOption(&opt); p.drawComplexControl(QStyle::CC_ToolButton, opt);}/*! \reimp */void QToolButton::actionEvent(QActionEvent *event){ Q_D(QToolButton); QAction *action = event->action(); switch (event->type()) { case QEvent::ActionChanged: if (action == d->defaultAction) setDefaultAction(action); // update button state break; case QEvent::ActionAdded: connect(action, SIGNAL(triggered()), this, SLOT(_q_actionTriggered())); break; case QEvent::ActionRemoved: if (d->defaultAction == action) d->defaultAction = 0;#ifndef QT_NO_MENU if (action == d->menuAction) d->menuAction = 0;#endif action->disconnect(this); break; default: ; } QAbstractButton::actionEvent(event);}void QToolButtonPrivate::_q_actionTriggered(){ Q_Q(QToolButton); if (QAction *action = qobject_cast<QAction *>(q->sender())) emit q->triggered(action);}/*! \reimp */void QToolButton::enterEvent(QEvent * e){ Q_D(QToolButton); if (d->autoRaise) update(); if (d->defaultAction) d->defaultAction->hover(); QAbstractButton::enterEvent(e);}/*! \reimp */void QToolButton::leaveEvent(QEvent * e){ Q_D(QToolButton); if (d->autoRaise) update(); QAbstractButton::leaveEvent(e);}/*! \reimp */void QToolButton::timerEvent(QTimerEvent *e){#ifndef QT_NO_MENU Q_D(QToolButton); if (e->timerId() == d->popupTimer.timerId()) { d->popupTimerDone(); return; }#endif QAbstractButton::timerEvent(e);}/*! \reimp*/void QToolButton::changeEvent(QEvent *e){#ifndef QT_NO_TOOLBAR Q_D(QToolButton); if (e->type() == QEvent::ParentChange) { if (qobject_cast<QToolBar*>(parentWidget())) d->autoRaise = true; } else if (e->type() == QEvent::StyleChange#ifdef Q_WS_MAC || e->type() == QEvent::MacSizeChange#endif ) {#ifdef QT3_SUPPORT if (!d->userDefinedPopupDelay)#endif d->delay = style()->styleHint(QStyle::SH_ToolButton_PopupDelay, 0, this); d->setLayoutItemMargins(QStyle::SE_ToolButtonLayoutItem); }#endif QAbstractButton::changeEvent(e);}/*! \reimp*/void QToolButton::mousePressEvent(QMouseEvent *e){ Q_D(QToolButton);#ifndef QT_NO_MENU QStyleOptionToolButton opt; initStyleOption(&opt); if (e->button() == Qt::LeftButton && d->popupMode == MenuButtonPopup) { QRect popupr = style()->subControlRect(QStyle::CC_ToolButton, &opt, QStyle::SC_ToolButtonMenu, this); if (popupr.isValid() && popupr.contains(e->pos())) { d->buttonPressed = QToolButtonPrivate::MenuButtonPressed; showMenu(); return; } }#endif d->buttonPressed = QToolButtonPrivate::ToolButtonPressed; QAbstractButton::mousePressEvent(e);}/*! \reimp*/void QToolButton::mouseReleaseEvent(QMouseEvent *e){ Q_D(QToolButton); QAbstractButton::mouseReleaseEvent(e); d->buttonPressed = QToolButtonPrivate::NoButtonPressed;}/*! \reimp*/bool QToolButton::hitButton(const QPoint &pos) const{ Q_D(const QToolButton); if(QAbstractButton::hitButton(pos)) return (d->buttonPressed != QToolButtonPrivate::MenuButtonPressed); return false;}#ifdef QT3_SUPPORT/*! Use icon() instead.*/QIcon QToolButton::onIconSet() const{ return icon();}/*! Use icon() instead.*/QIcon QToolButton::offIconSet() const{ return icon();}/*! \obsolete Use setIcon() instead.*/void QToolButton::setOnIconSet(const QIcon& set){ setIcon(set);}/*! \obsolete Use setIcon() instead.*/void QToolButton::setOffIconSet(const QIcon& set){ setIcon(set);}/*! \overload \obsolete Since Qt 3.0, QIcon contains both the On and Off icons. For ease of porting, this function ignores the \a on parameter and sets the \l icon property. If you relied on the \a on parameter, you probably want to update your code to use the QIcon On/Off mechanism. \sa icon QIcon::State*/void QToolButton::setIconSet(const QIcon & set, bool /* on */){ QAbstractButton::setIcon(set);}/*! \overload \obsolete Since Qt 3.0, QIcon contains both the On and Off icons. For ease of porting, this function ignores the \a on parameter and returns the \l icon property. If you relied on the \a on parameter, you probably want to update your code to use the QIcon On/Off mechanism.*/QIcon QToolButton::iconSet(bool /* on */) const{ return QAbstractButton::icon();}#endif#ifndef QT_NO_MENU/*! Associates the given \a menu with this tool button. The menu will be shown according to the button's \l popupMode. Ownership of the menu is not transferred to the tool button. \sa menu()*/void QToolButton::setMenu(QMenu* menu){ Q_D(QToolButton); if (d->menuAction) removeAction(d->menuAction); if (menu) { d->menuAction = menu->menuAction(); addAction(d->menuAction); } else { d->menuAction = 0; } update();}/*! Returns the associated menu, or 0 if no menu has been defined. \sa setMenu()*/QMenu* QToolButton::menu() const{ Q_D(const QToolButton); if (d->menuAction) return d->menuAction->menu(); return 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -