6.0.0-rc4Source

QTabWidget Class

The QTabWidget class is a stack of pages with a row of tabs to choose the page. More…

Header#include <QTabWidget>
CMaketarget_link_libraries(app PRIVATE Nt::Widgets)
ModuleNt Widgets C++ Classes
InheritsQWidget

List of all members, including inherited members

Public Functions

QTabWidget(QWidget *parent = nullptr)
~QTabWidget()
intaddTab(QWidget *page, const QString &label)
intinsertTab(int index, QWidget *page, const QString &label)
voidremoveTab(int index)
voidclear()
intcount() const
intcurrentIndex() const
QWidget *currentWidget() const
QWidget *widget(int index) const
intindexOf(const QWidget *page) const
QStringtabText(int index) const
voidsetTabText(int index, const QString &text)
boolisTabEnabled(int index) const
voidsetTabEnabled(int index, bool enabled)
QStringtabToolTip(int index) const
voidsetTabToolTip(int index, const QString &tip)
booltabsClosable() const
voidsetTabsClosable(bool closable)
boolisMovable() const
voidsetMovable(bool movable)
QTabBar *tabBar() const

Reimplemented Public Functions

Public Slots

voidsetCurrentIndex(int index)
voidsetCurrentWidget(QWidget *page)

Signals

voidcurrentChanged(int index)
voidtabCloseRequested(int index)
voidtabBarClicked(int index)
voidtabBarDoubleClicked(int index)

Reimplemented Protected Functions

voidpaintEvent(QPaintEvent *event)
voidresizeEvent(QResizeEvent *event)
voidchangeEvent(QEvent *event)

Detailed Description

Each page is a widget, usually with a layout of its own, that is shown when its tab is current. The user clicks a tab, or uses the Left and Right keys when the tabs have the focus, to switch:

QTabWidget *tabs = new QTabWidget(window);
tabs->addTab(buttonsPage, "Buttons");
tabs->addTab(inputPage, "Input");
connect(tabs, &QTabWidget::currentChanged, [](int index) { qDebug() << index; });

The tab widget makes the pages its children and sizes them to fill the area below the tabs. It does not own them in any other way than a parent owns its children: a page that is removed with removeTab() is not deleted, and deleting a page removes its tab.

A tab widget with the first tab current and the last one disabled.

QTabWidget in the light color scheme

See also QGroupBox and QStyle.

Differences from Qt

The tabs are always above the pages: there is no setTabPosition(), setTabShape(), setDocumentMode(), tab icons, corner widgets (setCornerWidget()), setTabBar() or setUsesScrollButtons(); tabs that do not fit are cut off. Ctrl+Tab does not switch tabs. A change of the size hint of a page does not update the layout the tab widget is in.

Member Function Documentation

[explicit] QTabWidget::QTabWidget(QWidget *parent = nullptr)

Constructs an empty tab widget with the given parent.

[override] QTabWidget::~QTabWidget()

Destroys the tab widget and the pages.

int QTabWidget::addTab(QWidget *page, const QString &label)

Adds page as a tab with the text label, after the other tabs, and returns its index. The tab widget becomes the parent of page. The first tab that is added becomes the current one.

See also insertTab().

[override, protected] void QTabWidget::changeEvent(QEvent *event)

Reimplements QWidget::changeEvent().

void QTabWidget::clear()

Removes all tabs. The pages are hidden but not deleted.

See also removeTab().

int QTabWidget::count() const

Returns the number of tabs.

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

This signal is emitted when the current tab changes to the one at index, which is -1 if there are no tabs left.

int QTabWidget::currentIndex() const

Returns the index of the current tab, or -1 if there are none.

See also setCurrentIndex().

QWidget * QTabWidget::currentWidget() const

Returns the page of the current tab, or nullptr if there are none.

See also setCurrentWidget().

int QTabWidget::indexOf(const QWidget *page) const

Returns the index of the tab that has page, or -1 if there is none.

See also widget().

int QTabWidget::insertTab(int index, QWidget *page, const QString &label)

Inserts page as a tab with the text label at index, which is brought into the range of the tabs, and returns the index of the new tab. The tab widget becomes the parent of page. The current tab stays the same one, except that the first tab that is added becomes the current one.

See also addTab() and removeTab().

bool QTabWidget::isMovable() const

Returns whether the user can drag the tabs into another order. The default is false.

See also setMovable().

bool QTabWidget::isTabEnabled(int index) const

Returns whether the tab at index can be chosen. The tabs are enabled at first.

See also setTabEnabled().

[override] QSize QTabWidget::minimumSizeHint() const

Reimplements QWidget::minimumSizeHint().

[override, protected] void QTabWidget::paintEvent(QPaintEvent *event)

Reimplements QWidget::paintEvent().

void QTabWidget::removeTab(int index)

Removes the tab at index. The page is hidden but not deleted, and stays a child of the tab widget. If the tab was the current one, a neighbouring tab becomes current.

See also clear() and insertTab().

[override, protected] void QTabWidget::resizeEvent(QResizeEvent *event)

Reimplements QWidget::resizeEvent().

[slot] void QTabWidget::setCurrentIndex(int index)

Makes the tab at index the current one and shows its page. An index that is not a tab is ignored. Emits currentChanged() if the tab changes.

See also currentIndex().

[slot] void QTabWidget::setCurrentWidget(QWidget *page)

Makes the tab of page the current one. Nothing happens if page is not in a tab.

See also currentWidget() and setCurrentIndex().

void QTabWidget::setMovable(bool movable)

Sets whether the user can drag the tabs into another order to movable. The pages move with their tabs.

void QTabWidget::setTabEnabled(int index, bool enabled)

Sets whether the tab at index can be chosen to enabled. A disabled tab is drawn grayed out and the user cannot switch to it. A tab that is current stays so.

See also isTabEnabled().

void QTabWidget::setTabsClosable(bool closable)

Sets whether the tabs have close buttons to closable. A click on one emits tabCloseRequested(); the tab widget does not remove the tab itself.

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

Sets the text of the tab at index to text.

See also tabText().

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

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

[override] QSize QTabWidget::sizeHint() const

Reimplements QWidget::sizeHint().

QTabBar * QTabWidget::tabBar() const

Returns the tab bar of the tab widget.

[signal] void QTabWidget::tabBarClicked(int index)

This signal is emitted when the user presses on the tab at index, or -1 for the empty part of the bar.

[signal] void QTabWidget::tabBarDoubleClicked(int index)

This signal is emitted when the user double-clicks the tab at index, or -1 for the empty part of the bar.

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

This signal is emitted when the user clicks the close button of the tab at index.

bool QTabWidget::tabsClosable() const

Returns whether the tabs have close buttons. The default is false.

See also setTabsClosable() and tabCloseRequested().

QString QTabWidget::tabText(int index) const

Returns the text of the tab at index, or an empty string if there is no such tab.

See also setTabText().

QString QTabWidget::tabToolTip(int index) const

Returns the tool tip of the tab at index.

See also setTabToolTip().

QWidget * QTabWidget::widget(int index) const

Returns the page of the tab at index, or nullptr if there is no such tab.

See also indexOf().