6.0.0-rc4Source

QTabBar Class

The QTabBar class provides a row of tabs. More…

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

List of all members, including inherited members

Public Types

Public Functions

QTabBar(QWidget *parent = nullptr)
~QTabBar()
intaddTab(const QString &text)
intinsertTab(int index, const QString &text)
voidremoveTab(int index)
voidmoveTab(int from, int to)
boolisTabEnabled(int index) const
voidsetTabEnabled(int index, bool enabled)
QStringtabText(int index) const
voidsetTabText(int index, const QString &text)
QStringtabToolTip(int index) const
voidsetTabToolTip(int index, const QString &tip)
QVarianttabData(int index) const
voidsetTabData(int index, const QVariant &data)
QRecttabRect(int index) const
inttabAt(const QPoint &position) const
intcurrentIndex() const
intcount() const
booltabsClosable() const
voidsetTabsClosable(bool closable)
boolisMovable() const
voidsetMovable(bool movable)
SelectionBehaviorselectionBehaviorOnRemove() const
voidsetSelectionBehaviorOnRemove(SelectionBehavior behavior)

Reimplemented Public Functions

Public Slots

voidsetCurrentIndex(int index)

Signals

voidcurrentChanged(int index)
voidtabCloseRequested(int index)
voidtabMoved(int from, int to)
voidtabBarClicked(int index)
voidtabBarDoubleClicked(int index)

Protected Functions

virtual QSizetabSizeHint(int index) const
virtual voidtabInserted(int index)
virtual voidtabRemoved(int index)
virtual voidtabLayoutChange()

Reimplemented Protected Functions

Detailed Description

A tab bar shows tabs with a text, one of which is current. QTabWidget uses one above its pages; a QTabBar on its own suits a view that changes with the tab:

QTabBar *bar = new QTabBar(window);
bar->addTab("Day");
bar->addTab("Week");
connect(bar, &QTabBar::currentChanged, [](int index) { showRange(index); });

The user clicks a tab, turns the mouse wheel over the bar, or uses Left, Right, Home and End while it has the focus; disabled tabs are skipped. With setTabsClosable() each tab has a close button that emits tabCloseRequested(), and a movable tab bar lets the user drag the tabs into another order.

See also QTabWidget.

Differences from Qt

The tabs are always at the top and rounded: there is no setShape(), no icons, no setTabButton(), setElideMode(), setExpanding(), setUsesScrollButtons(), setDrawBase() or setDocumentMode(), and no tab colors. Tabs that do not fit are cut off. The close button is drawn as an "x".

Member Type Documentation

enum QTabBar::SelectionBehavior

Which tab becomes current when the current tab is removed.

ConstantDescription
QTabBar::SelectLeftTabThe tab at its left.
QTabBar::SelectRightTabThe tab at its right, the default.
QTabBar::SelectPreviousTabThe tab that was current before it.

Member Function Documentation

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

Constructs an empty tab bar with the given parent.

[override] QTabBar::~QTabBar()

Destroys the tab bar.

int QTabBar::addTab(const QString &text)

Adds a tab with the given text at the end and returns its index. The first tab becomes current.

See also insertTab().

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

Reimplements QWidget::changeEvent().

int QTabBar::count() const

Returns the number of tabs.

[signal] void QTabBar::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 QTabBar::currentIndex() const

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

[override, protected] bool QTabBar::event(QEvent *event)

Reimplements QWidget::event().

A tool tip event over a tab shows the tool tip of the tab.

int QTabBar::insertTab(int index, const QString &text)

Inserts a tab with the given text at index, or at the end if index is out of range, and returns its index.

See also removeTab().

bool QTabBar::isMovable() const

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

bool QTabBar::isTabEnabled(int index) const

Returns whether the tab at index is enabled. A disabled tab cannot be made current by the user.

See also setTabEnabled().

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

Reimplements QWidget::keyPressEvent().

Left and Right move to the previous and next enabled tab, Home and End to the first and last.

[override, protected] void QTabBar::leaveEvent(QEvent *event)

Reimplements QWidget::leaveEvent().

[override] QSize QTabBar::minimumSizeHint() const

Reimplements QWidget::minimumSizeHint().

[override, protected] void QTabBar::mouseDoubleClickEvent(QMouseEvent *event)

Reimplements QWidget::mouseDoubleClickEvent().

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

Reimplements QWidget::mouseMoveEvent().

Dragging a tab of a movable tab bar past the middle of its neighbour swaps them.

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

Reimplements QWidget::mousePressEvent().

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

Reimplements QWidget::mouseReleaseEvent().

Releasing the mouse on the close button that was pressed emits tabCloseRequested().

void QTabBar::moveTab(int from, int to)

Moves the tab at from to to and emits tabMoved(). The current tab stays current.

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

Reimplements QWidget::paintEvent().

void QTabBar::removeTab(int index)

Removes the tab at index. If it was current, another tab becomes current as selectionBehaviorOnRemove() says.

See also insertTab().

SelectionBehavior QTabBar::selectionBehaviorOnRemove() const

Returns which tab becomes current when the current one is removed, SelectRightTab by default.

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

Makes the tab at index current and emits currentChanged(), if it is another tab.

See also currentIndex().

void QTabBar::setMovable(bool movable)

Sets whether the user can drag the tabs into another order to movable.

void QTabBar::setSelectionBehaviorOnRemove(SelectionBehavior behavior)

Sets which tab becomes current when the current one is removed to behavior.

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

Sets the data of the tab at index to data, for the application's own use.

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

Enables the tab at index if enabled is true, and disables it otherwise.

void QTabBar::setTabsClosable(bool closable)

Sets whether the tabs have close buttons to closable.

See also tabCloseRequested().

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

Sets the text of the tab at index to text.

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

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

[override] QSize QTabBar::sizeHint() const

Reimplements QWidget::sizeHint().

int QTabBar::tabAt(const QPoint &position) const

Returns the index of the tab at position, or -1 if there is none.

[signal] void QTabBar::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 QTabBar::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 QTabBar::tabCloseRequested(int index)

This signal is emitted when the user clicks the close button of the tab at index. The tab bar does not remove the tab itself.

QVariant QTabBar::tabData(int index) const

Returns the data of the tab at index.

See also setTabData().

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

Is called after a tab is inserted at index. The default implementation does nothing.

[virtual, protected] void QTabBar::tabLayoutChange()

Is called when the sizes of the tabs change. The default implementation does nothing.

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

This signal is emitted when the tab at from moves to to, by the user or by moveTab().

QRect QTabBar::tabRect(int index) const

Returns the rectangle of the tab at index in the tab bar, or an empty rectangle if there is no such tab.

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

Is called after the tab at index is removed. The default implementation does nothing.

bool QTabBar::tabsClosable() const

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

See also setTabsClosable().

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

Returns the size of the tab at index. Reimplement it for tabs of another size.

QString QTabBar::tabText(int index) const

Returns the text of the tab at index.

See also setTabText().

QString QTabBar::tabToolTip(int index) const

Returns the tool tip of the tab at index.

See also setTabToolTip().

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

Reimplements QWidget::wheelEvent().

The wheel moves to the previous or next enabled tab.