QTabBar Class
QTabBar 类提供了一个标签栏,例如可用于带标签的对话框。更多内容...
| 头文件: | #include <QTabBar> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Widgets) target_link_libraries(mytarget PRIVATE Qt6::Widgets) |
| qmake: | QT += widgets |
| 继承自: | QWidget |
公共类型
| enum | ButtonPosition { LeftSide, RightSide } |
| enum | SelectionBehavior { SelectLeftTab, SelectRightTab, SelectPreviousTab } |
| enum | Shape { RoundedNorth, RoundedSouth, RoundedWest, RoundedEast, TriangularNorth, …, TriangularEast } |
属性
|
|
公共函数
| QTabBar(QWidget *parent = nullptr) | |
| virtual | ~QTabBar() |
| QString | accessibleTabName(int index) const |
| int | addTab(const QString &text) |
| int | addTab(const QIcon &icon, const QString &text) |
| bool | autoHide() const |
| bool | changeCurrentOnDrag() const |
| int | count() const |
| int | currentIndex() const |
| bool | documentMode() const |
| bool | drawBase() const |
| Qt::TextElideMode | elideMode() const |
| bool | expanding() const |
| QSize | iconSize() const |
| int | insertTab(int index, const QString &text) |
| int | insertTab(int index, const QIcon &icon, const QString &text) |
| bool | isMovable() const |
| bool | isTabEnabled(int index) const |
| bool | isTabVisible(int index) const |
| void | moveTab(int from, int to) |
| void | removeTab(int index) |
| QTabBar::SelectionBehavior | selectionBehaviorOnRemove() const |
| void | setAccessibleTabName(int index, const QString &name) |
| void | setAutoHide(bool hide) |
| void | setChangeCurrentOnDrag(bool change) |
| void | setDocumentMode(bool set) |
| void | setDrawBase(bool drawTheBase) |
| void | setElideMode(Qt::TextElideMode mode) |
| void | setExpanding(bool enabled) |
| void | setIconSize(const QSize &size) |
| void | setMovable(bool movable) |
| void | setSelectionBehaviorOnRemove(QTabBar::SelectionBehavior behavior) |
| void | setShape(QTabBar::Shape shape) |
| void | setTabButton(int index, QTabBar::ButtonPosition position, QWidget *widget) |
| void | setTabData(int index, const QVariant &data) |
| void | setTabEnabled(int index, bool enabled) |
| void | setTabIcon(int index, const QIcon &icon) |
| void | setTabText(int index, const QString &text) |
| void | setTabTextColor(int index, const QColor &color) |
| void | setTabToolTip(int index, const QString &tip) |
| void | setTabVisible(int index, bool visible) |
| void | setTabWhatsThis(int index, const QString &text) |
| void | setTabsClosable(bool closable) |
| void | setUsesScrollButtons(bool useButtons) |
| QTabBar::Shape | shape() const |
| int | tabAt(const QPoint &position) const |
| QWidget * | tabButton(int index, QTabBar::ButtonPosition position) const |
| QVariant | tabData(int index) const |
| QIcon | tabIcon(int index) const |
| QRect | tabRect(int index) const |
| QString | tabText(int index) const |
| QColor | tabTextColor(int index) const |
| QString | tabToolTip(int index) const |
| QString | tabWhatsThis(int index) const |
| bool | tabsClosable() const |
| bool | usesScrollButtons() const |
重新实现的公共函数
| virtual QSize | minimumSizeHint() const override |
| virtual QSize | sizeHint() const override |
公共槽
| void | setCurrentIndex(int index) |
信号
| void | currentChanged(int index) |
| void | tabBarClicked(int index) |
| void | tabBarDoubleClicked(int index) |
| void | tabCloseRequested(int index) |
| void | tabMoved(int from, int to) |
受保护函数
| virtual void | initStyleOption(QStyleOptionTab *option, int tabIndex) const |
| virtual QSize | minimumTabSizeHint(int index) const |
| virtual void | tabInserted(int index) |
| virtual void | tabLayoutChange() |
| virtual void | tabRemoved(int index) |
| virtual QSize | tabSizeHint(int index) const |
重新实现的受保护函数
| virtual void | changeEvent(QEvent *event) override |
| virtual bool | event(QEvent *event) override |
| virtual void | hideEvent(QHideEvent *) override |
| virtual void | keyPressEvent(QKeyEvent *event) override |
| virtual void | mouseDoubleClickEvent(QMouseEvent *event) override |
| virtual void | mouseMoveEvent(QMouseEvent *event) override |
| virtual void | mousePressEvent(QMouseEvent *event) override |
| virtual void | mouseReleaseEvent(QMouseEvent *event) override |
| virtual void | paintEvent(QPaintEvent *) override |
| virtual void | resizeEvent(QResizeEvent *) override |
| virtual void | showEvent(QShowEvent *) override |
| virtual void | timerEvent(QTimerEvent *event) override |
| virtual void | wheelEvent(QWheelEvent *event) override |
详细说明
QTabBar 的使用非常简单;它使用预定义的shapes 之一绘制标签页,并在选中标签页时发出信号。可以通过子类化来定制其外观和风格。Qt 还提供了一个现成的QTabWidget 。
每个标签页都拥有tabText()、可选的tabIcon()、可选的tabToolTip()、可选的tabWhatsThis()以及可选的tabData()。可通过setTabText()、setTabIcon()、setTabToolTip()、setTabWhatsThis 和setTabData()修改标签页的属性。可通过setTabEnabled()单独启用或禁用每个标签页。
每个标签页均可显示不同颜色的文本。可通过tabTextColor() 函数获取标签页当前的文本颜色。使用setTabTextColor() 函数可设置特定标签页的文本颜色。
可通过addTab()添加标签页,或使用insertTab()将其插入特定位置。标签页的总数由count()返回。可通过removeTab()从标签栏中移除标签页。结合使用removeTab()和insertTab()可将标签页移动到不同位置。
shape 属性定义了选项卡的外观。形状的选择取决于个人喜好,尽管选项卡对话框(如首选项等)通常都使用RoundedNorth 。 在对话框以外的窗口中,标签控件几乎总是使用RoundedSouth 或TriangularSouth 。许多电子表格以及所有页面本质上相似的其他标签控件使用TriangularSouth ,而RoundedSouth 则主要在页面各不相同的情况下使用(例如多页工具面板)。QTabBar的默认设置是RoundedNorth 。
QTabBar API 中最重要的部分是currentChanged()信号。每当当前选项卡发生变化时(即使在启动时,当前选项卡从“none”变为其他状态时),该信号都会被触发。此外还有一个插槽setCurrentIndex(),可用于通过编程方式选择选项卡。 函数currentIndex() 返回当前标签页的索引,count 存储标签页的总数。
QTabBar 会按照QAbstractButton 中的方式自动生成助记键;例如,如果某个标签页的标签为“&Graphics”,则 Alt+G 将成为切换到该标签页的快捷键。
为了定制界面风格或在每个标签页中存储额外数据,可能需要重写以下虚拟函数:
- tabSizeHint() 计算标签页的大小。
- tabInserted() 通知已添加新标签页。
- tabRemoved() 通知已移除一个标签页。
- tabLayoutChange() 通知标签页布局已重新调整。
- paintEvent() 绘制所有标签页。
对于子类,您可能还需要tabRect()函数,该函数返回单个标签页的视觉几何信息。
![]() | 以Fusion 小部件样式显示的标签栏。 |
![]() | 以 Fusion 小部件样式显示的截断标签栏。 |
另请参阅 QTabWidget 。
成员类型文档
enum QTabBar::ButtonPosition
此枚举类型列出了小部件在选项卡上的位置。
| 常量 | 值 | 描述 |
|---|---|---|
QTabBar::LeftSide | 0 | 选项卡的左侧。 |
QTabBar::RightSide | 1 | 选项卡的右侧。 |
enum QTabBar::SelectionBehavior
此枚举类型列出了当删除一个标签页且该标签页同时也是当前标签页时,QTabBar 的行为。
| 常量 | 值 | 描述 |
|---|---|---|
QTabBar::SelectLeftTab | 0 | 选择被移除标签页左侧的标签页。 |
QTabBar::SelectRightTab | 1 | 选择被移除标签页右侧的标签页。 |
QTabBar::SelectPreviousTab | 2 | 选择之前选中的选项卡。 |
enum QTabBar::Shape
此枚举类型列出了QTabBar 支持的内置形状。请将这些视为参考,因为某些样式可能无法渲染部分形状。不过,位置应予以保留。
| 常量 | 值 | 描述 |
|---|---|---|
QTabBar::RoundedNorth | 0 | 页面上方的常规圆角样式 |
QTabBar::RoundedSouth | 1 | 页面下方的常规圆角样式 |
QTabBar::RoundedWest | 2 | 页面左侧的常规圆角样式 |
QTabBar::RoundedEast | 3 | 页面右侧的常规圆角样式 |
QTabBar::TriangularNorth | 4 | 页面上方的三角形标签。 |
QTabBar::TriangularSouth | 5 | 类似于Excel电子表格中使用的三角形标签,例如 |
QTabBar::TriangularWest | 6 | 页面左侧的三角形标签。 |
QTabBar::TriangularEast | 7 | 页面右侧的三角形标签。 |
属性文档
autoHide : bool
如果为 true,当标签栏中包含的标签少于 2 个时,该标签栏将自动隐藏。
默认情况下,此属性为 false。
访问函数:
| bool | autoHide() const |
| void | setAutoHide(bool hide) |
另请参阅 QWidget::visible 。
changeCurrentOnDrag : bool
如果为真,则在将鼠标悬停在标签栏上并拖动时,当前标签页会自动切换。
注意:您 还应将 acceptDrops 属性设置为 true,才能使此功能生效。
默认情况下,该属性为 false。
访问函数:
| bool | changeCurrentOnDrag() const |
| void | setChangeCurrentOnDrag(bool change) |
[read-only] count : int
该属性存储标签栏中的标签页数量
访问函数:
| int | count() const |
currentIndex : int
该属性存储标签栏中可见标签的索引
如果没有当前选中的标签页,则当前索引为 -1。
访问函数:
| int | currentIndex() const |
| void | setCurrentIndex(int index) |
通知信号:
| void | currentChanged(int index) |
documentMode : bool
该属性用于指定标签栏是否以适合主窗口的方式渲染。
该属性用作样式提示,用于以不同于标签控件中通常外观的方式绘制标签。在 macOS 上,其外观将类似于 Safari 或 Sierra 系统中 Terminal.app 中的标签。
访问函数:
| bool | documentMode() const |
| void | setDocumentMode(bool set) |
另请参阅 QTabWidget::documentMode 。
drawBase : bool
用于指定标签栏是否应绘制其底边。
如果为true,则QTabBar 会根据样式中的overlap属性绘制底板。否则,仅绘制标签。
访问函数:
| bool | drawBase() const |
| void | setDrawBase(bool drawTheBase) |
另请参阅 QStyle::pixelMetric()、QStyle::PM_TabBarBaseOverlap 以及QStyleOptionTabBarBase 。
elideMode : Qt::TextElideMode
如何在标签栏中省略文本
此属性控制当给定标签栏大小无法完全显示项目时,项目的省略方式。
默认情况下,该值取决于样式。
相关函数:
| Qt::TextElideMode | elideMode() const |
| void | setElideMode(Qt::TextElideMode mode) |
另请参阅 QTabWidget::elideMode 、usesScrollButtons 和QStyle::SH_TabBar_ElideMode 。
expanding : bool
当“扩展”设置为 true 时,QTabBar 会扩展标签页以利用空白区域。
默认值为 true。
访问函数:
| bool | expanding() const |
| void | setExpanding(bool enabled) |
另请参阅 QTabWidget::documentMode 。
iconSize : QSize
该属性用于指定标签栏中图标的大小
默认值取决于样式。iconSize 是最大尺寸;小于该尺寸的图标不会被放大。
访问函数:
| QSize | iconSize() const |
| void | setIconSize(const QSize &size) |
另请参阅 ` QTabWidget::iconSize`。
movable : bool
该属性控制用户是否可以在标签栏区域内移动标签页。
默认情况下,该属性的值为false ;
访问函数:
| bool | isMovable() const |
| void | setMovable(bool movable) |
selectionBehaviorOnRemove : SelectionBehavior
当调用 `removeTab ` 时,如果被移除的标签页也是当前标签页,应将哪个标签页设为当前标签页。
默认情况下,该值为SelectRightTab 。
访问函数:
| QTabBar::SelectionBehavior | selectionBehaviorOnRemove() const |
| void | setSelectionBehaviorOnRemove(QTabBar::SelectionBehavior behavior) |
另请参阅 removeTab()。
shape : Shape
该属性用于指定标签栏中标签的形状
该属性的可能取值由 Shape 枚举定义。
访问函数:
| QTabBar::Shape | shape() const |
| void | setShape(QTabBar::Shape shape) |
tabsClosable : bool
该属性控制标签栏是否应在每个标签上显示关闭按钮
当 `tabsClosable` 设置为 `true` 时,关闭按钮将根据样式显示在标签页的左侧或右侧。当直接点击该按钮,或在标签页的任意位置进行鼠标中键点击时,将触发 `tabCloseRequested ` 信号。
默认值为 false。
访问函数:
| bool | tabsClosable() const |
| void | setTabsClosable(bool closable) |
另请参阅 setTabButton() 和tabRemoved()。
usesScrollButtons : bool
该属性用于控制当标签栏包含大量标签时,是否应使用按钮来滚动标签。
当标签栏中的标签数量超过其大小所能容纳时,标签栏可以选择扩大其尺寸,或者添加按钮以便用户在标签之间滚动浏览。
默认情况下,该值取决于样式。
访问函数:
| bool | usesScrollButtons() const |
| void | setUsesScrollButtons(bool useButtons) |
另请参阅 elideMode 、QTabWidget::usesScrollButtons 和QStyle::SH_TabBar_PreferNoArrows 。
成员函数文档
[explicit] QTabBar::QTabBar(QWidget *parent = nullptr)
使用给定的parent 创建一个新的标签栏。
[virtual noexcept] QTabBar::~QTabBar()
删除标签栏。
QString QTabBar::accessibleTabName(int index) const
返回位于位置index 的选项卡的 accessibleName,如果index 超出范围,则返回空字符串。
另请参阅 setAccessibleTabName()。
int QTabBar::addTab(const QString &text)
添加一个标题为“text ”的新标签页。返回该新标签页的索引号。
int QTabBar::addTab(const QIcon &icon, const QString &text)
添加一个新标签页,其图标为icon ,文本为text 。返回新标签页的索引。
这是一个重载函数。
[override virtual protected] void QTabBar::changeEvent(QEvent *event)
重写了:QWidget::changeEvent(QEvent *event)。
[signal] void QTabBar::currentChanged(int index)
当标签栏的当前标签页发生变化时,会触发此信号。新的当前标签页具有给定的index ,如果没有新的当前标签页,则为-1(例如,当QTabBar 中没有标签页时)
注意: 这是属性 `currentIndex`的通知器 信号。
[override virtual protected] bool QTabBar::event(QEvent *event)
重写了:QWidget::event(QEvent *event)。
[override virtual protected] void QTabBar::hideEvent(QHideEvent *)
重写了:QWidget::hideEvent(QHideEvent *event)。
[virtual protected] void QTabBar::initStyleOption(QStyleOptionTab *option, int tabIndex) const
使用tabIndex 页面中的值初始化 `option `。当子类需要 `QStyleOptionTab`,但又不想自己填写所有信息时,此方法非常有用。
另请参阅 QStyleOption::initFrom() 和QTabWidget::initStyleOption()。
int QTabBar::insertTab(int index, const QString &text)
在位置index 处插入一个包含文本text 的新标签页。如果index 超出范围,则将新标签页追加到末尾。返回新标签页的索引。
int QTabBar::insertTab(int index, const QIcon &icon, const QString &text)
在位置index 处插入一个新标签页,其图标为icon ,文本为text 。如果index 超出范围,则将新标签页追加到末尾。返回新标签页的索引。
如果在调用此函数之前QTabBar 为空,则插入的标签页将成为当前标签页。
若在小于或等于当前索引的位置插入新标签页,则会递增当前索引,但保留当前标签页。
这是一个重载函数。
bool QTabBar::isTabEnabled(int index) const
如果位置为index 的标签处于启用状态,则返回true ;否则返回false 。
bool QTabBar::isTabVisible(int index) const
如果位置为index 的标签页可见,则返回true;否则返回false。
[override virtual protected] void QTabBar::keyPressEvent(QKeyEvent *event)
重写了:QWidget::keyPressEvent(QKeyEvent *event)。
[override virtual] QSize QTabBar::minimumSizeHint() const
重新实现了属性QWidget::minimumSizeHint 的访问函数。
[virtual protected] QSize QTabBar::minimumTabSizeHint(int index) const
返回位于位置index 的选项卡的最小选项卡大小提示值。
[override virtual protected] void QTabBar::mouseDoubleClickEvent(QMouseEvent *event)
重写了:QWidget::mouseDoubleClickEvent(QMouseEvent *event)。
[override virtual protected] void QTabBar::mouseMoveEvent(QMouseEvent *event)
重写了:QWidget::mouseMoveEvent(QMouseEvent *event)。
[override virtual protected] void QTabBar::mousePressEvent(QMouseEvent *event)
重写了:QWidget::mousePressEvent(QMouseEvent *event)。
[override virtual protected] void QTabBar::mouseReleaseEvent(QMouseEvent *event)
重写了:QWidget::mouseReleaseEvent(QMouseEvent *event)。
void QTabBar::moveTab(int from, int to)
将索引位置为from 的项目移动到索引位置to 。
另请参阅 tabMoved() 和tabLayoutChange()。
[override virtual protected] void QTabBar::paintEvent(QPaintEvent *)
重写了:QWidget::paintEvent(QPaintEvent *event)。
void QTabBar::removeTab(int index)
删除位于位置index 的标签页。
另请参阅 SelectionBehavior 。
[override virtual protected] void QTabBar::resizeEvent(QResizeEvent *)
重写了:QWidget::resizeEvent(QResizeEvent *event)。
void QTabBar::setAccessibleTabName(int index, const QString &name)
将位置为index 的选项卡的 accessibleName 设置为name 。
另请参阅 accessibleTabName()。
void QTabBar::setTabButton(int index, QTabBar::ButtonPosition position, QWidget *widget)
在“index ”选项卡上设置widget 。该控件将根据position 的设置,放置在左侧或右侧。
position 中先前设置的任何控件都将被隐藏。将widget 设置为nullptr 将隐藏当前位于position 的控件。
标签栏将拥有该小部件的所有权,因此除非您在设置其他小部件(或nullptr )后单独重新设置该小部件的父级,否则当标签栏被销毁时,此处设置的所有小部件都将被标签栏删除。
另请参阅 tabButton() 和tabsClosable()。
void QTabBar::setTabData(int index, const QVariant &data)
将位置为index 的标签页数据设置为data 。
另请参阅 tabData()。
void QTabBar::setTabEnabled(int index, bool enabled)
如果enabled 为真,则位置为index 的选项卡处于启用状态;否则,位置为index 的项目处于禁用状态。
另请参阅 isTabEnabled()。
void QTabBar::setTabIcon(int index, const QIcon &icon)
将位置为index 的标签页图标设置为icon 。
另请参阅 tabIcon()。
void QTabBar::setTabText(int index, const QString &text)
将位置为index 的选项卡文本设置为text 。
另请参阅 tabText()。
void QTabBar::setTabTextColor(int index, const QColor &color)
将具有指定index 的选项卡中文字的颜色设置为指定的color 。
如果指定的颜色无效,则选项卡将改用QTabBar 的前景色。
另请参阅 tabTextColor()。
void QTabBar::setTabToolTip(int index, const QString &tip)
将位置为index 的选项卡的工具提示设置为tip 。
另请参阅 tabToolTip()。
void QTabBar::setTabVisible(int index, bool visible)
如果visible 为真,则将位于位置index 的选项卡显示出来;否则将其隐藏。
另请参阅 isTabVisible()。
void QTabBar::setTabWhatsThis(int index, const QString &text)
将位置为index 的选项卡的“什么是这个”帮助文本设置为text 。
另请参阅 tabWhatsThis()。
[override virtual protected] void QTabBar::showEvent(QShowEvent *)
重写了:QWidget::showEvent(QShowEvent *event)。
[override virtual] QSize QTabBar::sizeHint() const
重新实现了属性QWidget::sizeHint 的访问函数。
int QTabBar::tabAt(const QPoint &position) const
返回覆盖position 的标签页的索引,如果没有标签页覆盖position ,则返回 -1;
[signal] void QTabBar::tabBarClicked(int index)
当用户单击index 上的一个标签页时,会触发此信号。
index 表示被点击的标签页的索引;如果光标未位于任何标签页上,则返回 -1。
[signal] void QTabBar::tabBarDoubleClicked(int index)
当用户双击index 上的标签页时,会触发此信号。
index 表示被点击的标签页;如果光标下方没有标签页,则返回 -1。
QWidget *QTabBar::tabButton(int index, QTabBar::ButtonPosition position) const
返回已设置标签页的控件集index ,若未设置标签页,则返回position 或nullptr 。
另请参阅 setTabButton()。
[signal] void QTabBar::tabCloseRequested(int index)
当点击标签页上的关闭按钮时,会触发此信号。index 表示应被移除的索引。
另请参阅 setTabsClosable()。
QVariant QTabBar::tabData(int index) const
返回位置为index 的标签页的数据,如果index 超出范围,则返回null变体。
另请参阅 setTabData()。
QIcon QTabBar::tabIcon(int index) const
返回位于位置 `index` 的选项卡图标;如果 `index ` 超出范围,则返回空图标。
另请参阅 setTabIcon()。
[virtual protected] void QTabBar::tabInserted(int index)
在index 位置添加或插入新标签页后,将调用此虚拟处理程序。
另请参阅 tabRemoved()。
[virtual protected] void QTabBar::tabLayoutChange()
每当标签页布局发生变化时,都会调用此虚拟处理程序。
另请参阅 tabRect()。
[signal] void QTabBar::tabMoved(int from, int to)
当标签页将索引位置为from 的标签页移动到索引位置为to 时,会发出此信号。
注意:当该信号从其标签栏发出时,QTabWidget 会自动移动页面。
另请参阅 moveTab()。
QRect QTabBar::tabRect(int index) const
返回位于位置index 处的选项卡的视觉矩形;如果index 被隐藏或超出范围,则返回空矩形。
[virtual protected] void QTabBar::tabRemoved(int index)
当从位置index 移除一个标签页后,将调用此虚拟处理程序。
另请参阅 tabInserted()。
[virtual protected] QSize QTabBar::tabSizeHint(int index) const
返回位于位置index 的选项卡的大小提示。
QString QTabBar::tabText(int index) const
返回位于位置index 的标签文本;如果index 超出范围,则返回空字符串。
另请参阅 setTabText()。
QColor QTabBar::tabTextColor(int index) const
返回具有给定index 的标签的文本颜色,如果index 超出范围,则返回无效颜色。
另请参阅 setTabTextColor()。
QString QTabBar::tabToolTip(int index) const
返回位于位置index 的选项卡的工具提示;如果index 超出范围,则返回空字符串。
另请参阅 setTabToolTip()。
QString QTabBar::tabWhatsThis(int index) const
返回位于位置index 的选项卡的“什么是这个”帮助文本;如果index 超出范围,则返回空字符串。
另请参阅 setTabWhatsThis()。
[override virtual protected] void QTabBar::timerEvent(QTimerEvent *event)
重写了:QObject::timerEvent(QTimerEvent *event)。
[override virtual protected] void QTabBar::wheelEvent(QWheelEvent *event)
重写了:QWidget::wheelEvent(QWheelEvent *event)。
© 2026 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.

