QToolButton Class

QToolButton 提供一个快速访问的命令或选项按钮, 通常在 QToolBar 中使用. 更多...

头文件: #include <QToolButton>
qmake: QT += widgets
基类: QAbstractButton

公有类型

enum ToolButtonPopupMode { DelayedPopup, MenuButtonPopup, InstantPopup }

属性

公有函数

QToolButton(QWidget *parent = Q_NULLPTR)
~QToolButton()
Qt::ArrowType arrowType() const
bool autoRaise() const
QAction *defaultAction() const
QMenu *menu() const
ToolButtonPopupMode popupMode() const
void setArrowType(Qt::ArrowType type)
void setAutoRaise(bool enable)
void setMenu(QMenu *menu)
void setPopupMode(ToolButtonPopupMode mode)
Qt::ToolButtonStyle toolButtonStyle() const

重新实现的公有函数

virtual QSize minimumSizeHint() const
virtual QSize sizeHint() const

公有槽函数

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

信号

void triggered(QAction *action)

受保护的函数

void initStyleOption(QStyleOptionToolButton *option) const

重新实现的受保护函数

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

其他继承的成员

  • 1 个公有变量继承自 QObject
  • 5 个静态公有成员继承自 QWidget
  • 10 个静态公有成员继承自 QObject
  • 1 个受保护的槽函数继承自 QWidget
  • 2 个受保护的变量继承自 QObject
  • 1 protected type inherited from QPaintDevice

详细描述

QToolButton 提供一个快速访问的命令或选项按钮, 通常在 QToolBar 中使用.

工具按钮是一种提供快速访问的命令或选项的特殊按钮. 与普通按钮不同, 工具按钮通常不显示文本, 仅显示一个图标.

当使用QToolBar::addAction()创建新的 QAction 实例或使用 QToolBar::addAction()添加现有实例时使用工具按钮. 你也可以类似其他widget的方式构造工具按钮, 并使用布局排布它与其他widget.

一个典型的用法是选择工具; 例如, 绘图程序中的"笔" 工具. 这个工具可以将 QToolButton 作为切换按钮实现 (参见 setCheckable()).

QToolButton 支持自动升起. 在自动升起模式下, 鼠标指向按钮时, 按钮绘制一个3D边框. 这个特性在 QToolBar自动开启. 调用 setAutoRaise()函数修改这个属性.

工具按钮的图标是 QIcon. 工具按钮支持不同的状态设置不同的图标. 按钮不可用时, 显示禁用图标. 鼠标悬浮在工具按钮上, 显示激活图标.

工具按钮的外观和尺寸可以调用 setToolButtonStyle() 和 setIconSize()设置. 当工具按钮被用于QMainWindowQToolBar 时 , 工具按钮自适应 QMainWindow的设置 (参见 QMainWindow::setToolButtonStyle() 和 QMainWindow::setIconSize()). 除了图标, 工具按钮也可显示一个 arrowType类型的箭头符号.

工具按钮支持弹出菜单, 提供额外选择. 调用 setMenu()设置弹出菜单. 调用 setPopupMode() 设置不同的弹出模式. 默认模式是DelayedPopupMode, 这种模式有时在浏览器中和 "Back" 按钮一起使用, 长按工具按钮时, 它会弹出一个菜单, 显示可跳转的页面列表. 超时时间由样式表设置, 参见 QStyle::SH_ToolButton_PopupDelay.

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.

参见 QPushButton, QToolBar, QMainWindow, QAction, and GUI Design Handbook: Push Button.

成员类型

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.

ConstantValueDescription
QToolButton::DelayedPopup0After pressing and holding the tool button down for a certain amount of time (the timeout is style dependent, 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
QToolButton::MenuButtonPopup1In 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.
QToolButton::InstantPopup2The menu is displayed, without delay, when the tool button is pressed. In this mode, the button's own action is not triggered.

属性

arrowType : Qt::ArrowType

This property holds whether the button displays an arrow instead of a normal icon

This displays an arrow as the icon for the QToolButton.

By default, this property is set to Qt::NoArrow.

访问函数:

Qt::ArrowType arrowType() const
void setArrowType(Qt::ArrowType type)

autoRaise : bool

This property holds whether auto-raising is enabled or not.

The default is disabled (i.e. false).

This property is currently ignored on macOS when using QMacStyle.

访问函数:

bool autoRaise() const
void setAutoRaise(bool enable)

popupMode : ToolButtonPopupMode

describes the way that popup menus are used with tool buttons

By default, this property is set to DelayedPopup.

访问函数:

ToolButtonPopupMode popupMode() const
void setPopupMode(ToolButtonPopupMode mode)

toolButtonStyle : Qt::ToolButtonStyle

This property holds whether the tool button displays an icon only, text only, or text beside/below the icon.

The default is Qt::ToolButtonIconOnly.

To have the style of toolbuttons follow the system settings, set this property to Qt::ToolButtonFollowStyle. On Unix, the user settings from the desktop environment will be used. On other platforms, Qt::ToolButtonFollowStyle means icon only.

QToolButton automatically connects this slot to the relevant signal in the QMainWindow in which is resides.

访问函数:

Qt::ToolButtonStyle toolButtonStyle() const
void setToolButtonStyle(Qt::ToolButtonStyle style)

成员函数

QToolButton::QToolButton(QWidget *parent = Q_NULLPTR)

Constructs an empty tool button with parent parent.

QToolButton::~QToolButton()

Destroys the object and frees any allocated resources.

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

重新实现 QWidget::actionEvent().

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

重新实现 QWidget::changeEvent().

QAction *QToolButton::defaultAction() const

Returns the default action.

参见 setDefaultAction().

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

重新实现 QWidget::enterEvent().

[virtual protected] bool QToolButton::event(QEvent *event)

重新实现 QObject::event().

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

重新实现 QAbstractButton::hitButton().

[protected] void QToolButton::initStyleOption(QStyleOptionToolButton *option) const

Initialize option with the values from this QToolButton. This method is useful for subclasses when they need a QStyleOptionToolButton, but don't want to fill in all the information themselves.

参见 QStyleOption::initFrom().

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

重新实现 QWidget::leaveEvent().

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

参见 setMenu().

[virtual] QSize QToolButton::minimumSizeHint() const

重新实现 QWidget::minimumSizeHint().

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

重新实现 QWidget::mousePressEvent().

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

重新实现 QWidget::mouseReleaseEvent().

[virtual protected] void QToolButton::nextCheckState()

重新实现 QAbstractButton::nextCheckState().

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

重新实现 QWidget::paintEvent().

Paints the button in response to the paint event.

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

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.

参见 defaultAction().

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.

参见 menu().

[slot] void QToolButton::showMenu()

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.

[virtual] QSize QToolButton::sizeHint() const

重新实现 QWidget::sizeHint().

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

重新实现 QObject::timerEvent().

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

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.