QTabBar Class
The QTabBar class provides a row of tabs. More…
| Header | #include <QTabBar> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Widgets) |
| Module | Nt Widgets C++ Classes |
| Inherits | QWidget |
List of all members, including inherited members
Public Types
Public Functions
| QTabBar(QWidget *parent = nullptr) | |
| ~QTabBar() | |
| int | addTab(const QString &text) |
| int | insertTab(int index, const QString &text) |
| void | removeTab(int index) |
| void | moveTab(int from, int to) |
| bool | isTabEnabled(int index) const |
| void | setTabEnabled(int index, bool enabled) |
| QString | tabText(int index) const |
| void | setTabText(int index, const QString &text) |
| QString | tabToolTip(int index) const |
| void | setTabToolTip(int index, const QString &tip) |
| QVariant | tabData(int index) const |
| void | setTabData(int index, const QVariant &data) |
| QRect | tabRect(int index) const |
| int | tabAt(const QPoint &position) const |
| int | currentIndex() const |
| int | count() const |
| bool | tabsClosable() const |
| void | setTabsClosable(bool closable) |
| bool | isMovable() const |
| void | setMovable(bool movable) |
| SelectionBehavior | selectionBehaviorOnRemove() const |
| void | setSelectionBehaviorOnRemove(SelectionBehavior behavior) |
Reimplemented Public Functions
| QSize | sizeHint() const |
| QSize | minimumSizeHint() const |
Public Slots
| void | setCurrentIndex(int index) |
Signals
| void | currentChanged(int index) |
| void | tabCloseRequested(int index) |
| void | tabMoved(int from, int to) |
| void | tabBarClicked(int index) |
| void | tabBarDoubleClicked(int index) |
Protected Functions
| virtual QSize | tabSizeHint(int index) const |
| virtual void | tabInserted(int index) |
| virtual void | tabRemoved(int index) |
| virtual void | tabLayoutChange() |
Reimplemented Protected Functions
| bool | event(QEvent *event) |
| void | paintEvent(QPaintEvent *event) |
| void | mousePressEvent(QMouseEvent *event) |
| void | mouseMoveEvent(QMouseEvent *event) |
| void | mouseReleaseEvent(QMouseEvent *event) |
| void | mouseDoubleClickEvent(QMouseEvent *event) |
| void | keyPressEvent(QKeyEvent *event) |
| void | wheelEvent(QWheelEvent *event) |
| void | leaveEvent(QEvent *event) |
| void | changeEvent(QEvent *event) |
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.
| Constant | Description |
|---|---|
QTabBar::SelectLeftTab | The tab at its left. |
QTabBar::SelectRightTab | The tab at its right, the default. |
QTabBar::SelectPreviousTab | The tab that was current before it. |
Member Function Documentation
QTabBar::QTabBar(QWidget *parent = nullptr)
Constructs an empty tab bar with the given parent.
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().
void QTabBar::changeEvent(QEvent *event)
Reimplements QWidget::changeEvent().
int QTabBar::count() const
Returns the number of tabs.
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.
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().
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.
void QTabBar::leaveEvent(QEvent *event)
Reimplements QWidget::leaveEvent().
QSize QTabBar::minimumSizeHint() const
Reimplements QWidget::minimumSizeHint().
void QTabBar::mouseDoubleClickEvent(QMouseEvent *event)
Reimplements QWidget::mouseDoubleClickEvent().
void QTabBar::mouseMoveEvent(QMouseEvent *event)
Reimplements QWidget::mouseMoveEvent().
Dragging a tab of a movable tab bar past the middle of its neighbour swaps them.
void QTabBar::mousePressEvent(QMouseEvent *event)
Reimplements QWidget::mousePressEvent().
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.
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.
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.
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.
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.
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.
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().
void QTabBar::tabInserted(int index)
Is called after a tab is inserted at index. The default implementation does nothing.
void QTabBar::tabLayoutChange()
Is called when the sizes of the tabs change. The default implementation does nothing.
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.
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().
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().
void QTabBar::wheelEvent(QWheelEvent *event)
Reimplements QWidget::wheelEvent().
The wheel moves to the previous or next enabled tab.