QTabBar Class Reference

The QTabBar class provides a tab bar, e.g. for use in tabbed dialogs. More...

 #include <QTabBar>

Inherits: QWidget.

Public Types

enum ButtonPosition { LeftSide, RightSide }
enum SelectionBehavior { SelectLeftTab, SelectRightTab, SelectPreviousTab }
enum Shape { RoundedNorth, RoundedSouth, RoundedWest, RoundedEast, ..., TriangularEast }

Properties

  • 58 properties inherited from QWidget
  • 1 property inherited from QObject

Public Functions

QTabBar ( QWidget * parent = 0 )
~QTabBar ()
int addTab ( const QString & text )
int addTab ( const QIcon & icon, const QString & text )
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 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

Reimplemented Public Functions

virtual QSize minimumSizeHint () const
virtual QSize sizeHint () const
  • 221 public functions inherited from QWidget
  • 29 public functions inherited from QObject
  • 13 public functions inherited from QPaintDevice

Public Slots

void setCurrentIndex ( int index )
  • 19 public slots inherited from QWidget
  • 1 public slot inherited from QObject

Signals

void currentChanged ( int index )
void tabCloseRequested ( int index )
void tabMoved ( int from, int to )

Protected Functions

void initStyleOption ( QStyleOptionTab * option, int tabIndex ) const
virtual void tabInserted ( int index )
virtual void tabLayoutChange ()
virtual void tabRemoved ( int index )
virtual QSize tabSizeHint ( int index ) const

Reimplemented Protected Functions

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 wheelEvent ( QWheelEvent * event )
  • 37 protected functions inherited from QWidget
  • 8 protected functions inherited from QObject
  • 1 protected function inherited from QPaintDevice

Additional Inherited Members

  • 4 static public members inherited from QWidget
  • 7 static public members inherited from QObject
  • 1 protected slot inherited from QWidget

Detailed Description

The QTabBar class provides a tab bar, e.g. for use in tabbed dialogs.

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:

For subclasses, you might also need the tabRect() functions which returns the visual geometry of a single tab.

Screenshot of a Plastique style tab barA tab bar shown in the Plastique widget style.
Screenshot of a truncated Plastique tab barA truncated tab bar shown in the Plastique widget style.

See also QTabWidget.

Member Type Documentation

enum QTabBar::ButtonPosition

This enum type lists the location of the widget on a tab.

ConstantValueDescription
QTabBar::LeftSide0Left side of the tab.
QTabBar::RightSide1Right 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.

ConstantValueDescription
QTabBar::SelectLeftTab0Select the tab to the left of the one being removed.
QTabBar::SelectRightTab1Select the tab to the right of the one being removed.
QTabBar::SelectPreviousTab2Select 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.

ConstantValueDescription
QTabBar::RoundedNorth0The normal rounded look above the pages
QTabBar::RoundedSouth1The normal rounded look below the pages
QTabBar::RoundedWest2The normal rounded look on the left side of the pages
QTabBar::RoundedEast3The normal rounded look on the right side the pages
QTabBar::TriangularNorth4Triangular tabs above the pages.
QTabBar::TriangularSouth5Triangular tabs similar to those used in the Excel spreadsheet, for example
QTabBar::TriangularWest6Triangular tabs on the left of the pages.
QTabBar::TriangularEast7Triangular tabs on the right of the pages.

Property Documentation

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 Mac OS X this will look similar to the tabs in Safari or Leopard'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

This property 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 QStyleOptionTabBarBaseV2.

elideMode : Qt::TextElideMode

This property holds 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 )

See also QTabWidget::elideMode, usesScrollButtons, and QStyle::SH_TabBar_ElideMode.

expanding : bool

This property holds 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 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

This property holds 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:

SelectionBehavior selectionBehaviorOnRemove () const
void setSelectionBehaviorOnRemove ( 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:

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.

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 dependant.

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.

Member Function Documentation

QTabBar::QTabBar ( QWidget * parent = 0 )

Creates a new tab bar with the given parent.

QTabBar::~QTabBar ()

Destroys the tab bar.

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.

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

Reimplemented from QWidget::changeEvent().

void QTabBar::currentChanged ( int index ) [signal]

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)

bool QTabBar::event ( QEvent * event ) [virtual protected]

Reimplemented from QObject::event().

void QTabBar::hideEvent ( QHideEvent * ) [virtual protected]

Reimplemented from QWidget::hideEvent().

void QTabBar::initStyleOption ( QStyleOptionTab * option, int tabIndex ) const [protected]

Initialize option with the values from the tab at tabIndex. This method is useful for subclasses when they need a QStyleOptionTab, QStyleOptionTabV2, or QStyleOptionTabV3 but don't want to fill in all the information themselves. This function will check the version of the QStyleOptionTab and fill in the additional values for a QStyleOptionTabV2 and QStyleOptionTabV3.

See also 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.

void QTabBar::keyPressEvent ( QKeyEvent * event ) [virtual protected]

Reimplemented from QWidget::keyPressEvent().

QSize QTabBar::minimumSizeHint () const [virtual]

Reimplemented from QWidget::minimumSizeHint().

void QTabBar::mouseMoveEvent ( QMouseEvent * event ) [virtual protected]

Reimplemented from QWidget::mouseMoveEvent().

void QTabBar::mousePressEvent ( QMouseEvent * event ) [virtual protected]

Reimplemented from QWidget::mousePressEvent().

void QTabBar::mouseReleaseEvent ( QMouseEvent * event ) [virtual protected]

Reimplemented from 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.

See also tabMoved() and tabLayoutChange().

void QTabBar::paintEvent ( QPaintEvent * ) [virtual protected]

Reimplemented from QWidget::paintEvent().

void QTabBar::removeTab ( int index )

Removes the tab at position index.

See also SelectionBehavior.

void QTabBar::resizeEvent ( QResizeEvent * ) [virtual protected]

Reimplemented from QWidget::resizeEvent().

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.

See also tabButton() and tabsClosable().

void QTabBar::setTabData ( int index, const QVariant & data )

Sets the data of the tab at position index to data.

See also 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.

See also isTabEnabled().

void QTabBar::setTabIcon ( int index, const QIcon & icon )

Sets the icon of the tab at position index to icon.

See also tabIcon().

void QTabBar::setTabText ( int index, const QString & text )

Sets the text of the tab at position index to text.

See also 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.

See also tabTextColor().

void QTabBar::setTabToolTip ( int index, const QString & tip )

Sets the tool tip of the tab at position index to tip.

See also 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.

See also tabWhatsThis().

void QTabBar::showEvent ( QShowEvent * ) [virtual protected]

Reimplemented from QWidget::showEvent().

QSize QTabBar::sizeHint () const [virtual]

Reimplemented from 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.

QWidget * QTabBar::tabButton ( int index, ButtonPosition position ) const

Returns the widget set a tab index and position or 0 if one is not set.

See also setTabButton().

void QTabBar::tabCloseRequested ( int index ) [signal]

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.

See also 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.

See also 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.

See also setTabIcon().

void QTabBar::tabInserted ( int index ) [virtual protected]

This virtual handler is called after a new tab was added or inserted at position index.

See also tabRemoved().

void QTabBar::tabLayoutChange () [virtual protected]

This virtual handler is called whenever the tab layout changes.

See also tabRect().

void QTabBar::tabMoved ( int from, int to ) [signal]

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.

See also 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.

void QTabBar::tabRemoved ( int index ) [virtual protected]

This virtual handler is called after a tab was removed from position index.

See also tabInserted().

QSize QTabBar::tabSizeHint ( int index ) const [virtual protected]

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.

See also 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.

See also 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.

See also 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.

See also setTabWhatsThis().

void QTabBar::wheelEvent ( QWheelEvent * event ) [virtual protected]

Reimplemented from QWidget::wheelEvent().

Notes provided by the Qt Community
Informative
  • 2

Votes: 2

Coverage: Qt library 4.7, 4.8, 5.0

Picture of tucnak tucnak

Ant Farmer
9 notes

QSS with QTabBar

You can give QTabBar external styling via QSS (Qt Style Sheets).

1) QTabBar style:

  1.   background-color: #000; /* black background */
  2. }

2) Style tab in QTabBar:

  1. QTabBar::tab { /* normal state */
  2.    background-image: url(":/img/a.png");
  3. }
  4.  
  5. QTabBar::tab:selected { /* selected tab state */
  6.    background-image: url(":/img/b.png");
  7. }

You also can use other pseudo-classes – hover, active.

3) Close button:

  1. QTabBar::close-button { /* normal */
  2.    background-image: url(":/img/button.png");
  3. }
  4.  
  5. QTabBar::close-button:hover { /* hover */
  6.    background-image: url(":/img/button-hover.png");
  7. }

Other pseudo-classes are allowed also.

With QTabBar and QSS you can create really cool tab interface in your application.

[Revisions]