QToolBar Class

QToolBar 类提供了一个包含一组控件的可移动面板。更多

Header: #include <QToolBar>
CMake: find_package(Qt6 REQUIRED COMPONENTS Widgets)
target_link_libraries(mytarget PRIVATE Qt6::Widgets)
qmake: QT += widgets
继承: QWidget

属性

公共功能

QToolBar(QWidget *parent = nullptr)
QToolBar(const QString &title, QWidget *parent = nullptr)
virtual ~QToolBar()
QAction *actionAt(const QPoint &p) const
QAction *actionAt(int x, int y) const
QAction *addSeparator()
QAction *addWidget(QWidget *widget)
Qt::ToolBarAreas allowedAreas() const
void clear()
QSize iconSize() const
QAction *insertSeparator(QAction *before)
QAction *insertWidget(QAction *before, QWidget *widget)
bool isAreaAllowed(Qt::ToolBarArea area) const
bool isFloatable() const
bool isFloating() const
bool isMovable() const
Qt::Orientation orientation() const
void setAllowedAreas(Qt::ToolBarAreas areas)
void setFloatable(bool floatable)
void setMovable(bool movable)
void setOrientation(Qt::Orientation orientation)
QAction *toggleViewAction() const
Qt::ToolButtonStyle toolButtonStyle() const
QWidget *widgetForAction(QAction *action) const

公共插槽

void setIconSize(const QSize &iconSize)
void setToolButtonStyle(Qt::ToolButtonStyle toolButtonStyle)

信号

void actionTriggered(QAction *action)
void allowedAreasChanged(Qt::ToolBarAreas allowedAreas)
void iconSizeChanged(const QSize &iconSize)
void movableChanged(bool movable)
void orientationChanged(Qt::Orientation orientation)
void toolButtonStyleChanged(Qt::ToolButtonStyle toolButtonStyle)
void topLevelChanged(bool topLevel)
void visibilityChanged(bool visible)

重新实现的受保护函数

virtual void actionEvent(QActionEvent *event) override
virtual void changeEvent(QEvent *event) override
virtual bool event(QEvent *event) override
virtual void paintEvent(QPaintEvent *event) override

详细说明

工具栏通常通过调用QMainWindow::addToolBar(const QString &title) 来创建,但也可以作为QVBoxLayout 中的第一个部件来添加。

工具栏按钮通过添加操作来添加,使用addAction() 或insertAction() 。可以使用addSeparator() 或insertSeparator() 分隔按钮组。如果工具栏按钮不合适,可以使用addWidget() 或insertWidget() 插入部件来代替。合适的小工具有QSpinBoxQDoubleSpinBoxQComboBox 。按下工具栏按钮时,它会发出actionTriggered() 信号。

工具栏可以固定在某一特定区域(如窗口顶部),也可以在工具栏区域之间移动;请参阅setMovable(),isMovable(),allowedAreas() 和isAreaAllowed() 。

当工具栏的大小调整到无法显示其包含的所有项目时,扩展按钮将作为工具栏的最后一个项目出现。按下扩展按钮将弹出一个菜单,其中包含当前工具栏中不合适的项目。

当 QToolBar 不是QMainWindow 的子工具栏时,它将失去使用addWidget() 添加到工具栏的部件来填充扩展弹出窗口的功能。请使用通过继承QWidgetAction 并实现QWidgetAction::createWidget() 而创建的部件操作。

另请参阅 QToolButtonQMenuQAction

属性文档

allowedAreas : Qt::ToolBarAreas

可放置工具栏的区域

默认为Qt::AllToolBarAreas

该属性只有在工具栏位于QMainWindow 时才有意义。

访问功能:

Qt::ToolBarAreas allowedAreas() const
void setAllowedAreas(Qt::ToolBarAreas areas)

Notifier 信号:

void allowedAreasChanged(Qt::ToolBarAreas allowedAreas)

另请参阅 movable

floatable : bool

该属性表示工具栏是否可以作为独立窗口拖放。

默认为 "true"。

访问功能:

bool isFloatable() const
void setFloatable(bool floatable)

[read-only] floating : const bool

该属性表示工具栏是否是一个独立窗口。

默认情况下,该属性为true

访问功能:

bool isFloating() const

另请参阅 QWidget::isWindow() 。

iconSize : QSize

工具栏中图标的大小。

默认大小由应用程序的样式决定,由QStyle::PM_ToolBarIconSize 像素度量得出。它是图标的最大尺寸。较小尺寸的图标不会被放大。

访问功能:

QSize iconSize() const
void setIconSize(const QSize &iconSize)

通知信号:

void iconSizeChanged(const QSize &iconSize)

movable : bool

用户是否可以在工具栏区域内或工具栏区域之间移动工具栏,由该属性决定。

默认情况下,该属性为true

该属性只有在工具栏位于QMainWindow 时才有意义。

访问功能:

bool isMovable() const
void setMovable(bool movable)

Notifier 信号:

void movableChanged(bool movable)

另请参阅 allowedAreas

orientation : Qt::Orientation

工具栏的方向

默认值为Qt::Horizontal

当工具栏由QMainWindow 管理时,不应使用此功能。如果要将已添加到主窗口的工具栏移动到另一个Qt::ToolBarArea ,可以使用QMainWindow::addToolBar() 或QMainWindow::insertToolBar() 。

访问功能:

Qt::Orientation orientation() const
void setOrientation(Qt::Orientation orientation)

通知信号:

void orientationChanged(Qt::Orientation orientation)

toolButtonStyle : Qt::ToolButtonStyle

该属性保存工具栏按钮的样式

该属性定义了所有作为QActions 添加的工具按钮的样式。请注意,如果使用addWidget() 方法添加QToolButton ,它将不会获得该按钮样式。

要使工具按钮的样式遵循系统设置,请将此属性设置为Qt::ToolButtonFollowStyle 。在 Unix 上,将使用桌面环境中的用户设置。在其他平台上,Qt::ToolButtonFollowStyle 只表示图标。

默认值为Qt::ToolButtonIconOnly

访问功能:

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

Notifier 信号:

void toolButtonStyleChanged(Qt::ToolButtonStyle toolButtonStyle)

成员函数 文档

[explicit] QToolBar::QToolBar(QWidget *parent = nullptr)

使用给定的parent 构建一个 QToolBar。

[explicit] QToolBar::QToolBar(const QString &title, QWidget *parent = nullptr)

用给定的parent 构造一个 QToolBar。

给定的窗口title 标识工具栏,并显示在QMainWindow 提供的上下文菜单中。

另请参阅 setWindowTitle() 。

[virtual noexcept] QToolBar::~QToolBar()

销毁工具栏。

QAction *QToolBar::actionAt(const QPoint &p) const

返回p 点的操作。如果未找到任何操作,该函数将返回 0。

另请参见 QWidget::childAt()。

QAction *QToolBar::actionAt(int x, int y) const

这是一个重载函数。

返回x,y 处的动作。如果未找到动作,该函数将返回 0。

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

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

[signal] void QToolBar::actionTriggered(QAction *action)

当触发该工具栏中的某个操作时,就会发出该信号。这发生在按下该操作的工具按钮时,或在工具栏外以其他方式触发该操作时。参数包含触发的action

QAction *QToolBar::addSeparator()

在工具栏末尾添加分隔符。

另请参见 insertSeparator()。

QAction *QToolBar::addWidget(QWidget *widget)

将给定的widget 添加到工具栏,作为工具栏的最后一项。

工具栏拥有widget 的所有权。

如果使用此方法添加QToolButton ,则不会尊重工具栏的Qt::ToolButtonStyle

注意: 您应该使用QAction::setVisible() 来更改窗口小部件的可见性。使用QWidget::setVisible(),QWidget::show() 和QWidget::hide() 将不起作用。

另请参见 insertWidget()。

[signal] void QToolBar::allowedAreasChanged(Qt::ToolBarAreas allowedAreas)

当工具栏允许的区域集合发生变化时,就会发出该信号。可定位工具栏的新区域由allowedAreas 指定。

注: 属性allowedAreas 的通知信号。

另请参见 allowedAreas

[override virtual protected] void QToolBar::changeEvent(QEvent *event)

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

void QToolBar::clear()

删除工具栏上的所有操作。

另请参阅 removeAction().

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

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

[signal] void QToolBar::iconSizeChanged(const QSize &iconSize)

当图标大小发生变化时会发出该信号。iconSize 参数包含工具栏的新图标尺寸。

注: 属性iconSize 的通知信号。

另请参阅 iconSizeQMainWindow::iconSize

QAction *QToolBar::insertSeparator(QAction *before)

在与before 操作相关的工具栏项目前的工具栏中插入分隔符。

另请参阅 addSeparator().

QAction *QToolBar::insertWidget(QAction *before, QWidget *widget)

在与before 操作相关联的工具栏项目前插入给定的widget

注意:应使用QAction::setVisible() 来更改部件的可见性。使用QWidget::setVisible(),QWidget::show() 和QWidget::hide() 将不起作用。

另请参阅 addWidget()。

bool QToolBar::isAreaAllowed(Qt::ToolBarArea area) const

如果该工具栏可停靠在给定的area 中,则返回true ;否则返回false

[signal] void QToolBar::movableChanged(bool movable)

当工具栏可移动或固定时,将发出该信号。如果工具栏可以移动,movable 为 true;否则为 false。

注: 属性movable 的通知信号。

另请参阅 movable

[signal] void QToolBar::orientationChanged(Qt::Orientation orientation)

当工具栏的方向发生变化时会发出该信号。orientation 参数包含工具栏的新方向。

注: 属性orientation 的通知信号。

另请参阅 orientation

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

重实现:QWidget::paintEvent(QPaintEvent *event).

QAction *QToolBar::toggleViewAction() const

返回一个可选中的操作,用于显示或隐藏该工具栏。

该操作的文本设置为工具栏的窗口标题。

另请参阅 QAction::textQWidget::windowTitle

[signal] void QToolBar::toolButtonStyleChanged(Qt::ToolButtonStyle toolButtonStyle)

该信号在工具按钮样式更改时发出。toolButtonStyle 参数包含工具栏的新工具按钮样式。

注: 属性toolButtonStyle 的通知信号。

另请参阅 toolButtonStyleQMainWindow::toolButtonStyle

[signal] void QToolBar::topLevelChanged(bool topLevel)

floating 属性发生变化时会发出该信号。如果工具栏现在是浮动的,则topLevel 参数为 true;否则为 false。

另请参见 isWindow().

[signal] void QToolBar::visibilityChanged(bool visible)

当工具栏变成visible (或不可见)时,就会发出该信号。这发生在部件隐藏或显示时。

QWidget *QToolBar::widgetForAction(QAction *action) const

返回与指定action 关联的 widget。

另请参阅 addWidget()。

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.