QToolButton 类

QToolButton 类提供命令或选项的快速访问按钮,通常用于 QToolBar . 更多...

头: #include <QToolButton>
qmake: QT += widgets
继承: QAbstractButton

公共类型

enum ToolButtonPopupMode { DelayedPopup, MenuButtonPopup, InstantPopup }

特性

公共函数

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

重实现公共函数

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

公共槽

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 ) override
virtual void changeEvent (QEvent * e ) override
virtual void enterEvent (QEvent * e ) override
virtual bool event (QEvent * event ) override
virtual bool hitButton (const QPoint & pos ) const override
virtual void leaveEvent (QEvent * e ) override
virtual void mousePressEvent (QMouseEvent * e ) override
virtual void mouseReleaseEvent (QMouseEvent * e ) override
virtual void nextCheckState () override
virtual void paintEvent (QPaintEvent * event ) override
virtual void timerEvent (QTimerEvent * e ) override

详细描述

工具按钮是能对特定命令 (或选项) 提供快速访问的特殊按钮。

通常,工具按钮的创建是在新的 QAction 实例被创建采用 QToolBar::addAction () 或现有动作被添加到工具栏采用 QToolBar::addAction ()。按如任何其它 Widget 的相同方式构造工具按钮,并将它们同其它小部件一起排列在布局中是可能的。

工具按钮的一种经典用法是选择工具;例如,绘图程序中的 pen 工具。这将通过使用 QToolButton 作为切换按钮 来实现 (见 setCheckable ()).

QToolButton 支持自动上升。在自动上升模式下,按钮仅当鼠标指到它时才绘制 3D 框架。特征会自动打开当按钮用于 QToolBar 。改变它采用 setAutoRaise ().

工具按钮的图标被设为 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.

按钮外观和尺度的调节采用 setToolButtonStyle () 和 setIconSize (). When used inside a QToolBar QMainWindow , the button automatically adjusts to QMainWindow 's settings (see QMainWindow::setToolButtonStyle () 和 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 ()。使用 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 timeout is style dependent, see 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 ,和 GUI 设计手册:按钮 .

成员类型文档编制

enum QToolButton:: ToolButtonPopupMode

描述拥有菜单集或包含动作列表的工具按钮应如何弹出菜单。

常量 描述
QToolButton::DelayedPopup 0 After 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::MenuButtonPopup 1 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.
QToolButton::InstantPopup 2 不延迟显示菜单,当按下工具按钮时。在此模式下,按钮自身的动作不会被触发。

特性文档编制

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 .

默认情况下,此特性被设为 Qt::NoArrow .

访问函数:

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

autoRaise : bool

此特性保持是否启用自动上升。

默认被禁用 (即:false)。

此特性在 macOS 目前是忽略的,当使用 QMacStyle 时。

访问函数:

bool autoRaise () const
void setAutoRaise (bool enable )

popupMode : ToolButtonPopupMode

描述用于工具按钮的菜单的弹出方式

默认情况下,此特性被设为 DelayedPopup .

访问函数:

QToolButton::ToolButtonPopupMode popupMode () const
void setPopupMode (QToolButton::ToolButtonPopupMode mode )

toolButtonStyle : Qt::ToolButtonStyle

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

默认为 Qt::ToolButtonIconOnly .

要让工具按钮样式遵循系统设置,将此特性设为 Qt::ToolButtonFollowStyle 。在 Unix,将使用来自桌面环境的用户设置。在其它平台, Qt::ToolButtonFollowStyle 意味着仅图标。

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 = nullptr)

构造空工具按钮采用父级 parent .

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

将默认动作设为 action .

若工具按钮拥有默认动作,则动作定义以下按钮特性:

其它特性,譬如 autoRepeat ,不受动作影响。与正常命令按钮相反,工具按钮通常不展示文本标签,取而代之展示图标。

另请参阅 defaultAction ().

[slot] void QToolButton:: showMenu ()

展示 (弹出) 关联的弹出菜单。若没有这种菜单,此函数什么都不做。此函数直到用户关闭弹出菜单后才返回。

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

此信号被发射当给定 action 被触发。

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.

[virtual] QToolButton:: ~QToolButton ()

销毁对象并释放任何分配资源。

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

重实现: QWidget::actionEvent (QActionEvent *event).

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

重实现: QAbstractButton::changeEvent (QEvent *e).

QAction *QToolButton:: defaultAction () const

返回默认动作。

另请参阅 setDefaultAction ().

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

重实现: QWidget::enterEvent (QEvent *event).

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

重实现: QAbstractButton::event (QEvent *e).

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

重实现: QAbstractButton::hitButton (const QPoint &pos) const.

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

初始化 option 采用值来自此 QToolButton 。此方法对子类是有用的,当需要 QStyleOptionToolButton ,但不希望自己填充所有信息。

另请参阅 QStyleOption::initFrom ().

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

重实现: QWidget::leaveEvent (QEvent *event).

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

另请参阅 setMenu ().

[override virtual] QSize QToolButton:: minimumSizeHint () const

重实现访问函数为特性: QWidget::minimumSizeHint .

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

重实现: QAbstractButton::mousePressEvent (QMouseEvent *e).

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

重实现: QAbstractButton::mouseReleaseEvent (QMouseEvent *e).

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

重实现: QAbstractButton::nextCheckState ().

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

重实现: QAbstractButton::paintEvent (QPaintEvent *e).

Paints the button in response to the paint event .

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 ().

[override virtual] QSize QToolButton:: sizeHint () const

重实现访问函数为特性: QWidget::sizeHint .

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

重实现: QAbstractButton::timerEvent (QTimerEvent *e).