QTabBar Class
The QTabBar class provides a tab bar, e.g. for use in tabbed dialogs. More...
| Header: | #include <QTabBar> |
| qmake: | QT += widgets |
| Inherits: | QWidget |
Public Types
| enum | ButtonPosition { LeftSide, RightSide } |
| enum | SelectionBehavior { SelectLeftTab, SelectRightTab, SelectPreviousTab } |
| enum | Shape { RoundedNorth, RoundedSouth, RoundedWest, RoundedEast, TriangularNorth, …, TriangularEast } |
Properties
|
|
Public Functions
| 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 |
| bool | isMovable() const |
| QTabBar::SelectionBehavior | selectionBehaviorOnRemove() const |
| 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 | setTabsClosable(bool closable) |
| void | setUsesScrollButtons(bool useButtons) |
| QTabBar::Shape | shape() const |
| bool | tabsClosable() const |
| bool | usesScrollButtons() const |
Public Slots
| void | setCurrentIndex(int index) |
Signals
| void | currentChanged(int index) |
Detailed Description
QTabBar is straightforward to use; it draws the tabs using one of the predefined shapes, and emits a signal when a tab is selected. It can be subclassed to tailor the look and feel. Qt also provides a ready-made QTabWidget.
Each tab has a tabText(), an optional tabIcon(), an optional tabToolTip(), optional tabWhatsThis() and optional tabData(). The tabs's attributes can be changed with setTabText(), setTabIcon(), setTabToolTip(), setTabWhatsThis and setTabData(). Each tabs can be enabled or disabled individually with setTabEnabled().
Each tab can display text in a distinct color. The current text color for a tab can be found with the tabTextColor() function. Set the text color for a particular tab with setTabTextColor().
Tabs are added using addTab(), or inserted at particular positions using insertTab(). The total number of tabs is given by count(). Tabs can be removed from the tab bar with removeTab(). Combining removeTab() and insertTab() allows you to move tabs to different positions.
The shape property defines the tabs' appearance. The choice of shape is a matter of taste, although tab dialogs (for preferences and similar) invariably use RoundedNorth. Tab controls in windows other than dialogs almost always use either RoundedSouth or TriangularSouth. Many spreadsheets and other tab controls in which all the pages are essentially similar use TriangularSouth, whereas RoundedSouth is used mostly when the pages are different (e.g. a multi-page tool palette). The default in QTabBar is RoundedNorth.
The most important part of QTabBar's API is the currentChanged() signal. This is emitted whenever the current tab changes (even at startup, when the current tab changes from 'none'). There is also a slot, setCurrentIndex(), which can be used to select a tab programmatically. The function currentIndex() returns the index of the current tab, count holds the number of tabs.
QTabBar creates automatic mnemonic keys in the manner of QAbstractButton; e.g. if a tab's label is "&Graphics", Alt+G becomes a shortcut key for switching to that tab.
The following virtual functions may need to be reimplemented in order to tailor the look and feel or store extra data with each tab:
- tabSizeHint() calcuates the size of a tab.
- tabInserted() notifies that a new tab was added.
- tabRemoved() notifies that a tab was removed.
- tabLayoutChange() notifies that the tabs have been re-laid out.
- paintEvent() paints all tabs.
For subclasses, you might also need the tabRect() functions which returns the visual geometry of a single tab.
![]() | A tab bar shown in the Fusion widget style. |
![]() | A truncated tab bar shown in the Fusion widget style. |
See also QTabWidget.
Member Type Documentation
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. |
Property Documentation
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.
Access functions:
| bool | autoHide() const |
| void | setAutoHide(bool hide) |
See also 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.
Access functions:
| bool | changeCurrentOnDrag() const |
| void | setChangeCurrentOnDrag(bool change) |
count : const int
This property holds the number of tabs in the tab bar
Access functions:
| 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.
Access functions:
| 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.
Access functions:
| bool | documentMode() const |
| void | setDocumentMode(bool set) |
See also QTabWidget::documentMode.
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.
Access functions:
| bool | drawBase() const |
| void | setDrawBase(bool drawTheBase) |
See also 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.
Access functions:
| Qt::TextElideMode | elideMode() const |
| void | setElideMode(Qt::TextElideMode mode) |
See also 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.
Access functions:
| bool | expanding() const |
| void | setExpanding(bool enabled) |
See also QTabWidget::documentMode.
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.
Access functions:
| QSize | iconSize() const |
| void | setIconSize(const QSize &size) |
See also QTabWidget::iconSize.
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.
Access functions:
| 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.
Access functions:
| QTabBar::SelectionBehavior | selectionBehaviorOnRemove() const |
| void | setSelectionBehaviorOnRemove(QTabBar::SelectionBehavior behavior) |
See also 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.
Access functions:
| QTabBar::Shape | shape() const |
| void | setShape(QTabBar::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.
Access functions:
| bool | tabsClosable() const |
| void | setTabsClosable(bool closable) |
See also 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-dependent.
This property was introduced in Qt 4.2.
Access functions:
| bool | usesScrollButtons() const |
| void | setUsesScrollButtons(bool useButtons) |
See also elideMode, QTabWidget::usesScrollButtons, and QStyle::SH_TabBar_PreferNoArrows.

