QTabBar Class
QTabBar 提供标签栏, 如. 用于标签对话框. 更多...
头文件: | #include <QTabBar> |
qmake: | QT += widgets |
基类: | QWidget |
公有类型
enum | ButtonPosition { LeftSide, RightSide } |
enum | SelectionBehavior { SelectLeftTab, SelectRightTab, SelectPreviousTab } |
enum | Shape { RoundedNorth, RoundedSouth, RoundedWest, RoundedEast, ..., TriangularEast } |
属性
|
|
公有函数
QTabBar(QWidget *parent = Q_NULLPTR) | |
~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 |
void | moveTab(int from, int to) |
void | removeTab(int index) |
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) |
void | setExpanding(bool enabled) |
void | setIconSize(const QSize &size) |
void | setMovable(bool movable) |
void | setSelectionBehaviorOnRemove(SelectionBehavior behavior) |
void | setShape(Shape shape) |
void | setTabButton(int index, 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 | setTabWhatsThis(int index, const QString &text) |
void | setTabsClosable(bool closable) |
void | setUsesScrollButtons(bool useButtons) |
Shape | shape() const |
int | tabAt(const QPoint &position) const |
QWidget * | tabButton(int index, 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 |
virtual QSize | sizeHint() const |
- 214 个公有函数继承自 QWidget
- 32 个公有函数继承自 QObject
- 14 个公有函数继承自 QPaintDevice
公有槽函数
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) |
受保护的函数
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) |
virtual bool | event(QEvent *event) |
virtual void | hideEvent(QHideEvent *) |
virtual void | keyPressEvent(QKeyEvent *event) |
virtual void | mouseMoveEvent(QMouseEvent *event) |
virtual void | mousePressEvent(QMouseEvent *event) |
virtual void | mouseReleaseEvent(QMouseEvent *event) |
virtual void | paintEvent(QPaintEvent *) |
virtual void | resizeEvent(QResizeEvent *) |
virtual void | showEvent(QShowEvent *) |
virtual void | timerEvent(QTimerEvent *event) |
virtual void | wheelEvent(QWheelEvent *event) |
- 35 个受保护的函数继承自 QWidget
- 9 个受保护的函数继承自 QObject
- 1 个受保护的函数继承自 QPaintDevice
其他继承的成员
- 1 个公有变量继承自 QObject
- 5 个静态公有成员继承自 QWidget
- 10 个静态公有成员继承自 QObject
- 1 个受保护的槽函数继承自 QWidget
- 2 个受保护的变量继承自 QObject
- 1 protected type inherited from QPaintDevice
详细描述
QTabBar 提供标签, 如. 用于标签对话框.
QTabBar 使用很简单; 它使用一个预定义 形状绘制自身, 选中标签时发出一个信号. 子类化QTabBar自定义外观. 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() 函数, 它返回单个标签的几何形状.
![]() | A tab bar shown in the Fusion widget style. |
![]() | A truncated tab bar shown in the Fusion widget style. |
参见 QTabWidget.
成员类型
enum QTabBar::ButtonPosition
This enum type lists the location of the widget on a tab.
Constant | Value | Description |
---|---|---|
QTabBar::LeftSide | 0 | Left side of the tab. |
QTabBar::RightSide | 1 | Right side of the tab. |
This enum was introduced or modified in Qt 4.5.
enum QTabBar::SelectionBehavior
This enum type lists the behavior of QTabBar when a tab is removed and the tab being removed is also the current tab.
Constant | Value | Description |
---|---|---|
QTabBar::SelectLeftTab | 0 | Select the tab to the left of the one being removed. |
QTabBar::SelectRightTab | 1 | Select the tab to the right of the one being removed. |
QTabBar::SelectPreviousTab | 2 | Select the previously selected tab. |
This enum was introduced or modified in Qt 4.5.
enum QTabBar::Shape
This enum type lists the built-in shapes supported by QTabBar. Treat these as hints as some styles may not render some of the shapes. However, position should be honored.
Constant | Value | Description |
---|---|---|
QTabBar::RoundedNorth | 0 | The normal rounded look above the pages |
QTabBar::RoundedSouth | 1 | The normal rounded look below the pages |
QTabBar::RoundedWest | 2 | The normal rounded look on the left side of the pages |
QTabBar::RoundedEast | 3 | The normal rounded look on the right side the pages |
QTabBar::TriangularNorth | 4 | Triangular tabs above the pages. |
QTabBar::TriangularSouth | 5 | Triangular tabs similar to those used in the Excel spreadsheet, for example |
QTabBar::TriangularWest | 6 | Triangular tabs on the left of the pages. |
QTabBar::TriangularEast | 7 | Triangular tabs on the right of the pages. |
属性
autoHide : bool
If true, the tab bar is automatically hidden when it contains less than 2 tabs.
By default, this property is false.
This property was introduced in Qt 5.4.
访问函数:
bool | autoHide() const |
void | setAutoHide(bool hide) |
参见 QWidget::visible.
changeCurrentOnDrag : bool
If true, then the current tab is automatically changed when dragging over the tabbar.
Note: You should also set acceptDrops property to true to make this feature work.
By default, this property is false.
This property was introduced in Qt 5.4.
访问函数:
bool | changeCurrentOnDrag() const |
void | setChangeCurrentOnDrag(bool change) |
count : const int
This property holds the number of tabs in the tab bar
访问函数:
int | count() const |
currentIndex : int
This property holds the index of the tab bar's visible tab
The current index is -1 if there is no current tab.
访问函数:
int | currentIndex() const |
void | setCurrentIndex(int index) |
Notifier signal:
void | currentChanged(int index) |
documentMode : bool
This property holds whether or not the tab bar is rendered in a mode suitable for the main window.
This property is used as a hint for styles to draw the tabs in a different way then they would normally look in a tab widget. On macOS this will look similar to the tabs in Safari or Sierra's Terminal.app.
This property was introduced in Qt 4.5.
访问函数:
bool | documentMode() const |
void | setDocumentMode(bool set) |
drawBase : bool
defines whether or not tab bar should draw its base.
If true then QTabBar draws a base in relation to the styles overlab. Otherwise only the tabs are drawn.
访问函数:
bool | drawBase() const |
void | setDrawBase(bool drawTheBase) |
参见 QStyle::pixelMetric(), QStyle::PM_TabBarBaseOverlap, and QStyleOptionTabBarBase.
elideMode : Qt::TextElideMode
how to elide text in the tab bar
This property controls how items are elided when there is not enough space to show them for a given tab bar size.
By default the value is style dependent.
This property was introduced in Qt 4.2.
访问函数:
Qt::TextElideMode | elideMode() const |
void | setElideMode(Qt::TextElideMode) |
参见 QTabWidget::elideMode, usesScrollButtons, and QStyle::SH_TabBar_ElideMode.
expanding : bool
When expanding is true QTabBar will expand the tabs to use the empty space.
By default the value is true.
This property was introduced in Qt 4.5.
访问函数:
bool | expanding() const |
void | setExpanding(bool enabled) |
iconSize : QSize
This property holds the size for icons in the tab bar
The default value is style-dependent. iconSize
is a maximum size; icons that are smaller are not scaled up.
This property was introduced in Qt 4.1.
访问函数:
QSize | iconSize() const |
void | setIconSize(const QSize &size) |
movable : bool
This property holds whether the user can move the tabs within the tabbar area.
By default, this property is false
;
This property was introduced in Qt 4.5.
访问函数:
bool | isMovable() const |
void | setMovable(bool movable) |
selectionBehaviorOnRemove : SelectionBehavior
What tab should be set as current when removeTab is called if the removed tab is also the current tab.
By default the value is SelectRightTab.
This property was introduced in Qt 4.5.
访问函数:
SelectionBehavior | selectionBehaviorOnRemove() const |
void | setSelectionBehaviorOnRemove(SelectionBehavior behavior) |
参见 removeTab().
shape : Shape
This property holds the shape of the tabs in the tab bar
Possible values for this property are described by the Shape enum.
访问函数:
Shape | shape() const |
void | setShape(Shape shape) |
tabsClosable : bool
This property holds whether or not a tab bar should place close buttons on each tab
When tabsClosable is set to true a close button will appear on the tab on either the left or right hand side depending upon the style. When the button is clicked the tab the signal tabCloseRequested will be emitted.
By default the value is false.
This property was introduced in Qt 4.5.
访问函数:
bool | tabsClosable() const |
void | setTabsClosable(bool closable) |
参见 setTabButton() and tabRemoved().
usesScrollButtons : bool
This property holds whether or not a tab bar should use buttons to scroll tabs when it has many tabs.
When there are too many tabs in a tab bar for its size, the tab bar can either choose to expand its size or to add buttons that allow you to scroll through the tabs.
By default the value is style dependant.
This property was introduced in Qt 4.2.
访问函数:
bool | usesScrollButtons() const |
void | setUsesScrollButtons(bool useButtons) |
参见 elideMode, QTabWidget::usesScrollButtons, and QStyle::SH_TabBar_PreferNoArrows.
成员函数
QTabBar::QTabBar(QWidget *parent = Q_NULLPTR)
Creates a new tab bar with the given parent.
QTabBar::~QTabBar()
Destroys the tab bar.
QString QTabBar::accessibleTabName(int index) const
Returns the accessibleName of the tab at position index, or an empty string if index is out of range.
参见 setAccessibleTabName().
int QTabBar::addTab(const QString &text)
Adds a new tab with text text. Returns the new tab's index.
int QTabBar::addTab(const QIcon &icon, const QString &text)
This is an overloaded function.
Adds a new tab with icon icon and text text. Returns the new tab's index.
[virtual protected]
void QTabBar::changeEvent(QEvent *event)
重新实现 QWidget::changeEvent().
[signal]
void QTabBar::currentChanged(int index)
This signal is emitted when the tab bar's current tab changes. The new current has the given index, or -1 if there isn't a new one (for example, if there are no tab in the QTabBar)
Note: Notifier signal for property currentIndex.
[virtual protected]
bool QTabBar::event(QEvent *event)
重新实现 QObject::event().
[virtual protected]
void QTabBar::hideEvent(QHideEvent *)
重新实现 QWidget::hideEvent().
[protected]
void QTabBar::initStyleOption(QStyleOptionTab *option, int tabIndex) const
Initialize option with the values from the tab at tabIndex. This method is useful for subclasses when they need a QStyleOptionTab, but don't want to fill in all the information themselves.
参见 QStyleOption::initFrom() and QTabWidget::initStyleOption().
int QTabBar::insertTab(int index, const QString &text)
Inserts a new tab with text text at position index. If index is out of range, the new tab is appened. Returns the new tab's index.
int QTabBar::insertTab(int index, const QIcon &icon, const QString &text)
This is an overloaded function.
Inserts a new tab with icon icon and text text at position index. If index is out of range, the new tab is appended. Returns the new tab's index.
If the QTabBar was empty before this function is called, the inserted tab becomes the current tab.
Inserting a new tab at an index less than or equal to the current index will increment the current index, but keep the current tab.
bool QTabBar::isTabEnabled(int index) const
Returns true
if the tab at position index is enabled; otherwise returns false
.
[virtual protected]
void QTabBar::keyPressEvent(QKeyEvent *event)
重新实现 QWidget::keyPressEvent().
[virtual]
QSize QTabBar::minimumSizeHint() const
重新实现 QWidget::minimumSizeHint().
[virtual protected]
QSize QTabBar::minimumTabSizeHint(int index) const
Returns the minimum tab size hint for the tab at position index.
This function was introduced in Qt 5.0.
[virtual protected]
void QTabBar::mouseMoveEvent(QMouseEvent *event)
重新实现 QWidget::mouseMoveEvent().
[virtual protected]
void QTabBar::mousePressEvent(QMouseEvent *event)
重新实现 QWidget::mousePressEvent().
[virtual protected]
void QTabBar::mouseReleaseEvent(QMouseEvent *event)
重新实现 QWidget::mouseReleaseEvent().
void QTabBar::moveTab(int from, int to)
Moves the item at index position from to index position to.
This function was introduced in Qt 4.5.
参见 tabMoved() and tabLayoutChange().
[virtual protected]
void QTabBar::paintEvent(QPaintEvent *)
重新实现 QWidget::paintEvent().
void QTabBar::removeTab(int index)
Removes the tab at position index.
[virtual protected]
void QTabBar::resizeEvent(QResizeEvent *)
重新实现 QWidget::resizeEvent().
void QTabBar::setAccessibleTabName(int index, const QString &name)
Sets the accessibleName of the tab at position index to name.
参见 accessibleTabName().
void QTabBar::setTabButton(int index, ButtonPosition position, QWidget *widget)
Sets widget on the tab index. The widget is placed on the left or right hand side depending upon the position.
Any previously set widget in position is hidden.
The tab bar will take ownership of the widget and so all widgets set here will be deleted by the tab bar when it is destroyed unless you separately reparent the widget after setting some other widget (or 0).
This function was introduced in Qt 4.5.
参见 tabButton() and tabsClosable().
void QTabBar::setTabData(int index, const QVariant &data)
Sets the data of the tab at position index to data.
参见 tabData().
void QTabBar::setTabEnabled(int index, bool enabled)
If enabled is true then the tab at position index is enabled; otherwise the item at position index is disabled.
参见 isTabEnabled().
void QTabBar::setTabIcon(int index, const QIcon &icon)
Sets the icon of the tab at position index to icon.
参见 tabIcon().
void QTabBar::setTabText(int index, const QString &text)
Sets the text of the tab at position index to text.
参见 tabText().
void QTabBar::setTabTextColor(int index, const QColor &color)
Sets the color of the text in the tab with the given index to the specified color.
If an invalid color is specified, the tab will use the QTabBar foreground role instead.
参见 tabTextColor().
void QTabBar::setTabToolTip(int index, const QString &tip)
Sets the tool tip of the tab at position index to tip.
参见 tabToolTip().
void QTabBar::setTabWhatsThis(int index, const QString &text)
Sets the What's This help text of the tab at position index to text.
This function was introduced in Qt 4.1.
参见 tabWhatsThis().
[virtual protected]
void QTabBar::showEvent(QShowEvent *)
重新实现 QWidget::showEvent().
[virtual]
QSize QTabBar::sizeHint() const
重新实现 QWidget::sizeHint().
int QTabBar::tabAt(const QPoint &position) const
Returns the index of the tab that covers position or -1 if no tab covers position;
This function was introduced in Qt 4.3.
[signal]
void QTabBar::tabBarClicked(int index)
This signal is emitted when user clicks on a tab at an index.
index is the index of a clicked tab, or -1 if no tab is under the cursor.
This function was introduced in Qt 5.2.
[signal]
void QTabBar::tabBarDoubleClicked(int index)
This signal is emitted when the user double clicks on a tab at index.
index refers to the tab clicked, or -1 if no tab is under the cursor.
This function was introduced in Qt 5.2.
QWidget *QTabBar::tabButton(int index, ButtonPosition position) const
Returns the widget set a tab index and position or 0 if one is not set.
参见 setTabButton().
[signal]
void QTabBar::tabCloseRequested(int index)
This signal is emitted when the close button on a tab is clicked. The index is the index that should be removed.
This function was introduced in Qt 4.5.
参见 setTabsClosable().
QVariant QTabBar::tabData(int index) const
Returns the data of the tab at position index, or a null variant if index is out of range.
参见 setTabData().
QIcon QTabBar::tabIcon(int index) const
Returns the icon of the tab at position index, or a null icon if index is out of range.
参见 setTabIcon().
[virtual protected]
void QTabBar::tabInserted(int index)
This virtual handler is called after a new tab was added or inserted at position index.
参见 tabRemoved().
[virtual protected]
void QTabBar::tabLayoutChange()
This virtual handler is called whenever the tab layout changes.
参见 tabRect().
[signal]
void QTabBar::tabMoved(int from, int to)
This signal is emitted when the tab has moved the tab at index position from to index position to.
note: QTabWidget will automatically move the page when this signal is emitted from its tab bar.
This function was introduced in Qt 4.5.
参见 moveTab().
QRect QTabBar::tabRect(int index) const
Returns the visual rectangle of the tab at position index, or a null rectangle if index is out of range.
[virtual protected]
void QTabBar::tabRemoved(int index)
This virtual handler is called after a tab was removed from position index.
参见 tabInserted().
[virtual protected]
QSize QTabBar::tabSizeHint(int index) const
Returns the size hint for the tab at position index.
QString QTabBar::tabText(int index) const
Returns the text of the tab at position index, or an empty string if index is out of range.
参见 setTabText().
QColor QTabBar::tabTextColor(int index) const
Returns the text color of the tab with the given index, or a invalid color if index is out of range.
参见 setTabTextColor().
QString QTabBar::tabToolTip(int index) const
Returns the tool tip of the tab at position index, or an empty string if index is out of range.
参见 setTabToolTip().
QString QTabBar::tabWhatsThis(int index) const
Returns the What's This help text of the tab at position index, or an empty string if index is out of range.
This function was introduced in Qt 4.1.
参见 setTabWhatsThis().
[virtual protected]
void QTabBar::timerEvent(QTimerEvent *event)
重新实现 QObject::timerEvent().
[virtual protected]
void QTabBar::wheelEvent(QWheelEvent *event)
重新实现 QWidget::wheelEvent().