|  | Home · All Classes · Modules

# QTabWidget Class Reference
[QtGui module]

The QTabWidget class provides a stack of tabbed widgets. More...

Inherits QWidget.

### Types

- enum TabPosition { North, South, West, East }
- enum TabShape { Rounded, Triangular }

### Methods

- __init__ (self, QWidget parent = None)
- int addTab (self, QWidget widget, QString)
- int addTab (self, QWidget widget, QIcon icon, QString label)
- changeEvent (self, QEvent)
- clear (self)
- QWidget cornerWidget (self, Qt.Corner corner = Qt.TopRightCorner)
- int count (self)
- int currentIndex (self)
- QWidget currentWidget (self)
- bool documentMode (self)
- Qt.TextElideMode elideMode (self)
- bool event (self, QEvent)
- QSize iconSize (self)
- int indexOf (self, QWidget widget)
- initStyleOption (self, QStyleOptionTabWidgetFrame option)
- int insertTab (self, int index, QWidget widget, QString)
- int insertTab (self, int index, QWidget widget, QIcon icon, QString label)
- bool isMovable (self)
- bool isTabEnabled (self, int index)
- keyPressEvent (self, QKeyEvent)
- QSize minimumSizeHint (self)
- paintEvent (self, QPaintEvent)
- removeTab (self, int index)
- resizeEvent (self, QResizeEvent)
- setCornerWidget (self, QWidget w, Qt.Corner corner = Qt.TopRightCorner)
- setCurrentIndex (self, int index)
- setCurrentWidget (self, QWidget widget)
- setDocumentMode (self, bool set)
- setElideMode (self, Qt.TextElideMode)
- setIconSize (self, QSize size)
- setMovable (self, bool movable)
- setTabBar (self, QTabBar)
- setTabEnabled (self, int index, bool)
- setTabIcon (self, int index, QIcon icon)
- setTabPosition (self, TabPosition)
- setTabsClosable (self, bool closeable)
- setTabShape (self, TabShape s)
- setTabText (self, int index, QString)
- setTabToolTip (self, int index, QString tip)
- setTabWhatsThis (self, int index, QString text)
- setUsesScrollButtons (self, bool useButtons)
- showEvent (self, QShowEvent)
- QSize sizeHint (self)
- QTabBar tabBar (self)
- QIcon tabIcon (self, int index)
- tabInserted (self, int index)
- TabPosition tabPosition (self)
- tabRemoved (self, int index)
- bool tabsClosable (self)
- TabShape tabShape (self)
- QString tabText (self, int index)
- QString tabToolTip (self, int index)
- QString tabWhatsThis (self, int index)
- bool usesScrollButtons (self)
- QWidget widget (self, int index)

### Qt Signals

- void currentChanged (int)
- void tabCloseRequested (int)

## Detailed Description

The QTabWidget class provides a stack of tabbed widgets.

A tab widget provides a tab bar (see QTabBar) and a "page area" that is used to display pages related to each tab. By default, the tab bar is shown above the page area, but different configurations are available (see TabPosition). Each tab is associated with a different widget (called a page). Only the current page is shown in the page area; all the other pages are hidden. The user can show a different page by clicking on its tab or by pressing its Alt+letter shortcut if it has one.

The normal way to use QTabWidget is to do the following:

1. Create a QTabWidget.
2. Create a QWidget for each of the pages in the tab dialog, but do not specify parent widgets for them.
3. Insert child widgets into the page widget, using layouts to position them as normal.
4. Call addTab() or insertTab() to put the page widgets into the tab widget, giving each tab a suitable label with an optional keyboard shortcut.

The position of the tabs is defined by tabPosition, their shape by tabShape.

The signal currentChanged() is emitted when the user selects a page.

The current page index is available as currentIndex(), the current page widget with currentWidget(). You can retrieve a pointer to a page widget with a given index using widget(), and can find the index position of a widget with indexOf(). Use setCurrentWidget() or setCurrentIndex() to show a particular page.

You can change a tab's text and icon using setTabText() or setTabIcon(). A tab and its associated page can be removed with removeTab().

Each tab is either enabled or disabled at any given time (see setTabEnabled()). If a tab is enabled, the tab text is drawn normally and the user can select that tab. If it is disabled, the tab is drawn in a different way and the user cannot select that tab. Note that even if a tab is disabled, the page can still be visible, for example if all of the tabs happen to be disabled.

Tab widgets can be a very good way to split up a complex dialog. An alternative is to use a QStackedWidget for which you provide some means of navigating between pages, for example, a QToolBar or a QListWidget.

Most of the functionality in QTabWidget is provided by a QTabBar (at the top, providing the tabs) and a QStackedWidget (most of the area, organizing the individual pages).

A Windows XP style tab widget. | A Macintosh style tab widget. | A Plastique style tab widget.

See also QTabBar, QStackedWidget, QToolBox, and Tab Dialog Example.

## Type Documentation

### QTabWidget.TabPosition

This enum type defines where QTabWidget draws the tab row:

Constant | Value | Description
--- | --- | ---
`QTabWidget.North` | `0` | The tabs are drawn above the pages.
`QTabWidget.South` | `1` | The tabs are drawn below the pages.
`QTabWidget.West` | `2` | The tabs are drawn to the left of the pages.
`QTabWidget.East` | `3` | The tabs are drawn to the right of the pages.

### QTabWidget.TabShape

This enum type defines the shape of the tabs:

Constant | Value | Description
--- | --- | ---
`QTabWidget.Rounded` | `0` | The tabs are drawn with a rounded look. This is the default shape.
`QTabWidget.Triangular` | `1` | The tabs are drawn with a triangular look.

## Method Documentation

### QTabWidget.__init__ (self, QWidget parent = None)

The parent argument, if not None, causes self to be owned by Qt instead of PyQt.

Constructs a tabbed widget with parent parent.

### int QTabWidget.addTab (self, QWidget widget, QString)

The widget argument has it's ownership transferred to Qt.

Adds a tab with the given page and label to the tab widget, and returns the index of the tab in the tab bar.

If the tab's label contains an ampersand, the letter following the ampersand is used as a shortcut for the tab, e.g. if the label is "Bro&wse" then Alt+W becomes a shortcut which will move the focus to this tab.

Note: If you call addTab() after show(), the layout system will try to adjust to the changes in its widgets hierarchy and may cause flicker. To prevent this, you can set the QWidget.updatesEnabled property to false prior to changes; remember to set the property to true when the changes are done, making the widget receive paint events again.

See also insertTab().

### int QTabWidget.addTab (self, QWidget widget, QIcon icon, QString label)

The widget argument has it's ownership transferred to Qt.

This is an overloaded function.

Adds a tab with the given page, icon, and label to the tab widget, and returns the index of the tab in the tab bar.

This function is the same as addTab(), but with an additional icon.

### QTabWidget.changeEvent (self, QEvent)

### QTabWidget.clear (self)

Removes all the pages, but does not delete them. Calling this function is equivalent to calling removeTab() until the tab widget is empty.

### QWidget QTabWidget.cornerWidget (self, Qt.Corner corner = Qt.TopRightCorner)

Returns the widget shown in the corner of the tab widget or 0.

See also setCornerWidget().

### int QTabWidget.count (self)

### int QTabWidget.currentIndex (self)

### QWidget QTabWidget.currentWidget (self)

Returns a pointer to the page currently being displayed by the tab dialog. The tab dialog does its best to make sure that this value is never 0 (but if you try hard enough, it can be).

See also currentIndex() and setCurrentWidget().

### bool QTabWidget.documentMode (self)

### Qt.TextElideMode QTabWidget.elideMode (self)

### bool QTabWidget.event (self, QEvent)

### QSize QTabWidget.iconSize (self)

### int QTabWidget.indexOf (self, QWidget widget)

Returns the index position of the page occupied by the widget w, or -1 if the widget cannot be found.

### QTabWidget.initStyleOption (self, QStyleOptionTabWidgetFrame option)

Initialize option with the values from this QTabWidget. This method is useful for subclasses when they need a QStyleOptionTabWidgetFrame, but don't want to fill in all the information themselves.

See also QStyleOption.initFrom() and QTabBar.initStyleOption().

### int QTabWidget.insertTab (self, int index, QWidget widget, QString)

The widget argument has it's ownership transferred to Qt.

Inserts a tab with the given label and page into the tab widget at the specified index, and returns the index of the inserted tab in the tab bar.

The label is displayed in the tab and may vary in appearance depending on the configuration of the tab widget.

If the tab's label contains an ampersand, the letter following the ampersand is used as a shortcut for the tab, e.g. if the label is "Bro&wse" then Alt+W becomes a shortcut which will move the focus to this tab.

If index is out of range, the tab is simply appended. Otherwise it is inserted at the specified position.

If the QTabWidget was empty before this function is called, the new page becomes the current page. Inserting a new tab at an index less than or equal to the current index will increment the current index, but keep the current page.

Note: If you call insertTab() after show(), the layout system will try to adjust to the changes in its widgets hierarchy and may cause flicker. To prevent this, you can set the QWidget.updatesEnabled property to false prior to changes; remember to set the property to true when the changes are done, making the widget receive paint events again.

See also addTab().

### int QTabWidget.insertTab (self, int index, QWidget widget, QIcon icon, QString label)

The widget argument has it's ownership transferred to Qt.

### bool QTabWidget.isMovable (self)

### bool QTabWidget.isTabEnabled (self, int index)

Returns true if the page at position index is enabled; otherwise returns false.

See also setTabEnabled() and QWidget.isEnabled().

### QTabWidget.keyPressEvent (self, QKeyEvent)

### QSize QTabWidget.minimumSizeHint (self)

### QTabWidget.paintEvent (self, QPaintEvent)

Paints the tab widget's tab bar in response to the paint event.

Reimplemented from QWidget.

### QTabWidget.removeTab (self, int index)

Removes the tab at position index from this stack of widgets. The page widget itself is not deleted.

See also addTab() and insertTab().

### QTabWidget.resizeEvent (self, QResizeEvent)

### QTabWidget.setCornerWidget (self, QWidget w, Qt.Corner corner = Qt.TopRightCorner)

The w argument has it's ownership transferred to Qt.

Sets the given widget to be shown in the specified corner of the tab widget. The geometry of the widget is determined based on the widget's sizeHint() and the style().

Only the horizontal element of the corner will be used.

Passing 0 shows no widget in the corner.

Any previously set corner widget is hidden.

All widgets set here will be deleted by the tab widget when it is destroyed unless you separately reparent the widget after setting some other corner widget (or 0).

Note: Corner widgets are designed for North and South tab positions; other orientations are known to not work properly.

See also cornerWidget() and setTabPosition().

### QTabWidget.setCurrentIndex (self, int index)

This method is also a Qt slot with the C++ signature `void setCurrentIndex(int)`.

### QTabWidget.setCurrentWidget (self, QWidget widget)

This method is also a Qt slot with the C++ signature `void setCurrentWidget(QWidget *)`.

Makes widget the current widget. The widget used must be a page in this tab widget.

See also addTab(), setCurrentIndex(), and currentWidget().

### QTabWidget.setDocumentMode (self, bool set)

### QTabWidget.setElideMode (self, Qt.TextElideMode)

### QTabWidget.setIconSize (self, QSize size)

### QTabWidget.setMovable (self, bool movable)

### QTabWidget.setTabBar (self, QTabBar)

The QTabBar argument has it's ownership transferred to Qt.

Replaces the dialog's QTabBar heading with the tab bar tb. Note that this must be called before any tabs have been added, or the behavior is undefined.

See also tabBar().

### QTabWidget.setTabEnabled (self, int index, bool)

If enable is true, the page at position index is enabled; otherwise the page at position index is disabled. The page's tab is redrawn appropriately.

QTabWidget uses QWidget.setEnabled() internally, rather than keeping a separate flag.

Note that even a disabled tab/page may be visible. If the page is visible already, QTabWidget will not hide it; if all the pages are disabled, QTabWidget will show one of them.

See also isTabEnabled() and QWidget.setEnabled().

### QTabWidget.setTabIcon (self, int index, QIcon icon)

This is an overloaded function.

Sets the icon for the tab at position index.

See also tabIcon().

### QTabWidget.setTabPosition (self, TabPosition)

### QTabWidget.setTabsClosable (self, bool closeable)

### QTabWidget.setTabShape (self, TabShape s)

### QTabWidget.setTabText (self, int index, QString)

Defines a new label for the page at position index's tab.

If the provided text contains an ampersand character ('&'), a shortcut is automatically created for it. The character that follows the '&' will be used as the shortcut key. Any previous shortcut will be overwritten, or cleared if no shortcut is defined by the text. See the QShortcut documentation for details (to display an actual ampersand, use '&&').

See also tabText().

### QTabWidget.setTabToolTip (self, int index, QString tip)

Sets the tab tool tip for the page at position index to tip.

See also tabToolTip().

### QTabWidget.setTabWhatsThis (self, int index, QString text)

Sets the What's This help text for the page at position index to text.

This function was introduced in Qt 4.1.

See also tabWhatsThis().

### QTabWidget.setUsesScrollButtons (self, bool useButtons)

### QTabWidget.showEvent (self, QShowEvent)

### QSize QTabWidget.sizeHint (self)

### QTabBar QTabWidget.tabBar (self)

Returns the current QTabBar.

See also setTabBar().

### QIcon QTabWidget.tabIcon (self, int index)

Returns the icon for the tab on the page at position index.

See also setTabIcon().

### QTabWidget.tabInserted (self, int index)

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

See also tabRemoved().

### TabPosition QTabWidget.tabPosition (self)

### QTabWidget.tabRemoved (self, int index)

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

See also tabInserted().

### bool QTabWidget.tabsClosable (self)

### TabShape QTabWidget.tabShape (self)

### QString QTabWidget.tabText (self, int index)

Returns the label text for the tab on the page at position index.

See also setTabText().

### QString QTabWidget.tabToolTip (self, int index)

Returns the tab tool tip for the page at position index or an empty string if no tool tip has been set.

See also setTabToolTip().

### QString QTabWidget.tabWhatsThis (self, int index)

Returns the What's This help text for the page at position index, or an empty string if no help text has been set.

This function was introduced in Qt 4.1.

See also setTabWhatsThis().

### bool QTabWidget.usesScrollButtons (self)

### QWidget QTabWidget.widget (self, int index)

Returns the tab page at index position index or 0 if the index is out of range.

## Qt Signal Documentation

### void currentChanged (int)

This is the default overload of this signal.

This signal is emitted whenever the current page index changes. The parameter is the new current page index position, or -1 if there isn't a new one (for example, if there are no widgets in the QTabWidget)

See also currentWidget() and currentIndex.

### void tabCloseRequested (int)

This is the default overload of this 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().