QTabWidget

The QTabWidget class provides a stack of tabbed widgets. More

Inheritance diagram of PySide2.QtWidgets.QTabWidget

Synopsis

Functions

Virtual functions

Slots

Signals

Detailed Description

../../_images/windows-tabwidget.png

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

See also

QTabBar QStackedWidget QToolBox Tab Dialog Example

class QTabWidget([parent=None])
param parent

QWidget

Constructs a tabbed widget with parent parent .

PySide2.QtWidgets.QTabWidget.TabPosition

This enum type defines where QTabWidget draws the tab row:

Constant

Description

QTabWidget.North

The tabs are drawn above the pages.

QTabWidget.South

The tabs are drawn below the pages.

QTabWidget.West

The tabs are drawn to the left of the pages.

QTabWidget.East

The tabs are drawn to the right of the pages.

PySide2.QtWidgets.QTabWidget.TabShape

This enum type defines the shape of the tabs:

Constant

Description

QTabWidget.Rounded

The tabs are drawn with a rounded look. This is the default shape.

QTabWidget.Triangular

The tabs are drawn with a triangular look.

PySide2.QtWidgets.QTabWidget.addTab(widget, icon, label)
Parameters
  • widgetQWidget

  • iconQIcon

  • label – unicode

Return type

int

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. Ownership of page is passed on to the QTabWidget .

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

PySide2.QtWidgets.QTabWidget.addTab(widget, arg__2)
Parameters
  • widgetQWidget

  • arg__2 – unicode

Return type

int

Adds a tab with the given page and label to the tab widget, and returns the index of the tab in the tab bar. Ownership of page is passed on to the QTabWidget .

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 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 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()

PySide2.QtWidgets.QTabWidget.clear()

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

PySide2.QtWidgets.QTabWidget.cornerWidget([corner=Qt.TopRightCorner])
Parameters

cornerCorner

Return type

QWidget

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

PySide2.QtWidgets.QTabWidget.count()
Return type

int

PySide2.QtWidgets.QTabWidget.currentChanged(index)
Parameters

indexint

PySide2.QtWidgets.QTabWidget.currentIndex()
Return type

int

PySide2.QtWidgets.QTabWidget.currentWidget()
Return type

QWidget

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

PySide2.QtWidgets.QTabWidget.documentMode()
Return type

bool

PySide2.QtWidgets.QTabWidget.elideMode()
Return type

TextElideMode

See also

setElideMode()

PySide2.QtWidgets.QTabWidget.iconSize()
Return type

QSize

See also

setIconSize()

PySide2.QtWidgets.QTabWidget.indexOf(widget)
Parameters

widgetQWidget

Return type

int

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

PySide2.QtWidgets.QTabWidget.initStyleOption(option)
Parameters

optionQStyleOptionTabWidgetFrame

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.

PySide2.QtWidgets.QTabWidget.insertTab(index, widget, icon, label)
Parameters
  • indexint

  • widgetQWidget

  • iconQIcon

  • label – unicode

Return type

int

This is an overloaded function.

Inserts a tab with the given label , page , and icon into the tab widget at the specified index , and returns the index of the inserted tab in the tab bar. Ownership of page is passed on to the QTabWidget .

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

PySide2.QtWidgets.QTabWidget.insertTab(index, widget, arg__3)
Parameters
  • indexint

  • widgetQWidget

  • arg__3 – unicode

Return type

int

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. Ownership of page is passed on to the QTabWidget .

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 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 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()

PySide2.QtWidgets.QTabWidget.isMovable()
Return type

bool

PySide2.QtWidgets.QTabWidget.isTabEnabled(index)
Parameters

indexint

Return type

bool

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

PySide2.QtWidgets.QTabWidget.removeTab(index)
Parameters

indexint

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

PySide2.QtWidgets.QTabWidget.setCornerWidget(w[, corner=Qt.TopRightCorner])
Parameters

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 None ).

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

PySide2.QtWidgets.QTabWidget.setCurrentIndex(index)
Parameters

indexint

See also

currentIndex()

PySide2.QtWidgets.QTabWidget.setCurrentWidget(widget)
Parameters

widgetQWidget

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

PySide2.QtWidgets.QTabWidget.setDocumentMode(set)
Parameters

setbool

See also

documentMode()

PySide2.QtWidgets.QTabWidget.setElideMode(arg__1)
Parameters

arg__1TextElideMode

See also

elideMode()

PySide2.QtWidgets.QTabWidget.setIconSize(size)
Parameters

sizeQSize

See also

iconSize()

PySide2.QtWidgets.QTabWidget.setMovable(movable)
Parameters

movablebool

See also

isMovable()

PySide2.QtWidgets.QTabWidget.setTabBar(arg__1)
Parameters

arg__1QTabBar

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()

PySide2.QtWidgets.QTabWidget.setTabBarAutoHide(enabled)
Parameters

enabledbool

See also

tabBarAutoHide()

PySide2.QtWidgets.QTabWidget.setTabEnabled(index, arg__2)
Parameters
  • indexint

  • arg__2bool

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

PySide2.QtWidgets.QTabWidget.setTabIcon(index, icon)
Parameters
  • indexint

  • iconQIcon

Sets the icon for the tab at position index .

See also

tabIcon()

PySide2.QtWidgets.QTabWidget.setTabPosition(arg__1)
Parameters

arg__1TabPosition

See also

tabPosition()

PySide2.QtWidgets.QTabWidget.setTabShape(s)
Parameters

sTabShape

See also

tabShape()

PySide2.QtWidgets.QTabWidget.setTabText(index, arg__2)
Parameters
  • indexint

  • arg__2 – unicode

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()

PySide2.QtWidgets.QTabWidget.setTabToolTip(index, tip)
Parameters
  • indexint

  • tip – unicode

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

See also

tabToolTip()

PySide2.QtWidgets.QTabWidget.setTabWhatsThis(index, text)
Parameters
  • indexint

  • text – unicode

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

See also

tabWhatsThis()

PySide2.QtWidgets.QTabWidget.setTabsClosable(closeable)
Parameters

closeablebool

See also

tabsClosable()

PySide2.QtWidgets.QTabWidget.setUsesScrollButtons(useButtons)
Parameters

useButtonsbool

PySide2.QtWidgets.QTabWidget.tabBar()
Return type

QTabBar

Returns the current QTabBar .

See also

setTabBar()

PySide2.QtWidgets.QTabWidget.tabBarAutoHide()
Return type

bool

PySide2.QtWidgets.QTabWidget.tabBarClicked(index)
Parameters

indexint

PySide2.QtWidgets.QTabWidget.tabBarDoubleClicked(index)
Parameters

indexint

PySide2.QtWidgets.QTabWidget.tabCloseRequested(index)
Parameters

indexint

PySide2.QtWidgets.QTabWidget.tabIcon(index)
Parameters

indexint

Return type

QIcon

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

See also

setTabIcon()

PySide2.QtWidgets.QTabWidget.tabInserted(index)
Parameters

indexint

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

See also

tabRemoved()

PySide2.QtWidgets.QTabWidget.tabPosition()
Return type

TabPosition

See also

setTabPosition()

PySide2.QtWidgets.QTabWidget.tabRemoved(index)
Parameters

indexint

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

See also

tabInserted()

PySide2.QtWidgets.QTabWidget.tabShape()
Return type

TabShape

See also

setTabShape()

PySide2.QtWidgets.QTabWidget.tabText(index)
Parameters

indexint

Return type

unicode

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

See also

setTabText()

PySide2.QtWidgets.QTabWidget.tabToolTip(index)
Parameters

indexint

Return type

unicode

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()

PySide2.QtWidgets.QTabWidget.tabWhatsThis(index)
Parameters

indexint

Return type

unicode

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

PySide2.QtWidgets.QTabWidget.tabsClosable()
Return type

bool

PySide2.QtWidgets.QTabWidget.usesScrollButtons()
Return type

bool

PySide2.QtWidgets.QTabWidget.widget(index)
Parameters

indexint

Return type

QWidget

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