QTabBar Class
QTabBar 类提供了一个标签栏,例如用于标签式对话框。更多
Header: | #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 XML 还提供了一个现成的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() 信号。每当当前标签页发生变化时(甚至在启动时,当前标签页从 "无 "变为 "无 "时),都会发出该信号。还有一个插槽setCurrentIndex() 可用于以编程方式选择标签页。函数currentIndex() 返回当前标签页的索引,count 保存标签页的数量。
QTabBar 以QAbstractButton 的方式创建自动记忆键;例如,如果标签页的标签是"&Graphics",Alt+G 就会成为切换到该标签页的快捷键。
以下虚拟函数可能需要重新实现,以便为每个选项卡定制外观或存储额外数据:
- tabSizeHint() 计算标签页的大小。
- tabInserted() 通知已添加新标签页。
- tabRemoved() 通知删除了一个标签页。
- tabLayoutChange() 通知标签已被重新排列。
- paintEvent() 会涂抹所有标签页。
对于子类,您可能还需要tabRect() 函数,该函数用于返回单个选项卡的可视几何图形。
![]() | 以Fusion widget 风格显示的标签栏。 |
![]() | 以 Fusion widget 风格显示的截断标签栏。 |
另请参阅 QTabWidget 。
成员类型文档
enum QTabBar::ButtonPosition
该枚举类型列出了 Widget 在标签页上的位置。
常数 | 值 | 描述 |
---|---|---|
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 个时,标签栏会自动隐藏。
默认情况下,此属性为假。
访问功能:
bool | autoHide() const |
void | setAutoHide(bool hide) |
另请参阅 QWidget::visible 。
changeCurrentOnDrag : bool
如果为 true,则在标签栏上拖动时会自动更改当前标签。
注意: 还应将 acceptDrops 属性设置为 true,以使此功能生效。
默认情况下,该属性为 false。
访问功能:
bool | changeCurrentOnDrag() const |
void | setChangeCurrentOnDrag(bool change) |
[read-only]
count : const int
该属性用于保存标签栏中标签的数量
访问功能
int | count() const |
currentIndex : int
该属性保存标签栏可见标签的索引。
如果没有当前标签页,则当前索引为-1。
访问功能:
int | currentIndex() const |
void | setCurrentIndex(int index) |
Notifier 信号:
void | currentChanged(int index) |
documentMode : bool
该属性表示是否以适合主窗口的模式呈现标签栏。
该属性用于提示样式以不同于标签 Widget 通常的方式绘制标签。在 macOS 上,这将类似于 Safari 或 Sierra 的 Terminal.app 中的标签。
访问功能:
bool | documentMode() const |
void | setDocumentMode(bool set) |
另请参见 QTabWidget::documentMode 。
drawBase : bool
定义标签栏是否应绘制基底。
如果为 "true",则QTabBar 会根据样式重叠绘制底座。否则只绘制标签。
访问函数:
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
该属性用于保存标签栏中标签的形状。
形状枚举描述了该属性的可能值。
访问功能
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 上标签页的可访问名称,如果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 都会被隐藏。将widget 设置为nullptr
将隐藏当前 widgetposition 。
标签栏将拥有该 widget 的所有权,因此当标签栏被销毁时,在此设置的所有 widget 都将被标签栏删除,除非您在设置其他 widget(或nullptr
)后单独重新指定该 widget。
另请参阅 tabButton() 和tabsClosable()。
void QTabBar::setTabData(int index, const QVariant &data)
将index 位置的制表符数据设置为data 。
另请参阅 tabData() 。
void QTabBar::setTabEnabled(int index, bool enabled)
如果enabled 为 true,则启用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 为 true,则使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 标签的 widget,如果未设置,则返回nullptr
。
另请参阅 setTabButton().
[signal]
void QTabBar::tabCloseRequested(int index)
点击标签页上的关闭按钮时会发出该信号。index 是应删除的索引。
另请参阅 setTabsClosable() 。
QVariant QTabBar::tabData(int index) const
返回index 位置的制表符数据,如果index 不在范围内,则返回空变量。
另请参阅 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 位置的标签页的帮助文本 What's This,如果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).
© 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.