QTabWidget Class

QTabWidget 提供一组标签页. 更多...

头文件: #include <QTabWidget>
qmake: QT += widgets
基类: QWidget

公有类型

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

属性

公有函数

QTabWidget(QWidget *parent = Q_NULLPTR)
~QTabWidget()
int addTab(QWidget *page, const QString &label)
int addTab(QWidget *page, const QIcon &icon, const QString &label)
void clear()
QWidget *cornerWidget(Qt::Corner corner = Qt::TopRightCorner) const
int count() const
int currentIndex() const
QWidget *currentWidget() const
bool documentMode() const
Qt::TextElideMode elideMode() const
QSize iconSize() const
int indexOf(QWidget *w) const
int insertTab(int index, QWidget *page, const QString &label)
int insertTab(int index, QWidget *page, const QIcon &icon, const QString &label)
bool isMovable() const
bool isTabEnabled(int index) const
void removeTab(int index)
void setCornerWidget(QWidget *widget, Qt::Corner corner = Qt::TopRightCorner)
void setDocumentMode(bool set)
void setElideMode(Qt::TextElideMode)
void setIconSize(const QSize &size)
void setMovable(bool movable)
void setTabBarAutoHide(bool enabled)
void setTabEnabled(int index, bool enable)
void setTabIcon(int index, const QIcon &icon)
void setTabPosition(TabPosition)
void setTabShape(TabShape s)
void setTabText(int index, const QString &label)
void setTabToolTip(int index, const QString &tip)
void setTabWhatsThis(int index, const QString &text)
void setTabsClosable(bool closeable)
void setUsesScrollButtons(bool useButtons)
QTabBar *tabBar() const
bool tabBarAutoHide() const
QIcon tabIcon(int index) const
TabPosition tabPosition() const
TabShape tabShape() const
QString tabText(int index) const
QString tabToolTip(int index) const
QString tabWhatsThis(int index) const
bool tabsClosable() const
bool usesScrollButtons() const
QWidget *widget(int index) const

重新实现的公有函数

virtual bool hasHeightForWidth() const
virtual int heightForWidth(int width) const
virtual QSize minimumSizeHint() const
virtual QSize sizeHint() const

公有槽函数

void setCurrentIndex(int index)
void setCurrentWidget(QWidget *widget)
  • 19 个公有槽函数继承自 QWidget
  • 1 个公有槽函数继承自 QObject

信号

void currentChanged(int index)
void tabBarClicked(int index)
void tabBarDoubleClicked(int index)
void tabCloseRequested(int index)

受保护的函数

void initStyleOption(QStyleOptionTabWidgetFrame *option) const
void setTabBar(QTabBar *tb)
virtual void tabInserted(int index)
virtual void tabRemoved(int index)

重新实现的受保护函数

virtual void changeEvent(QEvent *ev)
virtual bool event(QEvent *ev)
virtual void keyPressEvent(QKeyEvent *e)
virtual void paintEvent(QPaintEvent *event)
virtual void resizeEvent(QResizeEvent *e)
virtual void showEvent(QShowEvent *)
  • 35 个受保护的函数继承自 QWidget
  • 9 个受保护的函数继承自 QObject
  • 1 个受保护的函数继承自 QPaintDevice

其他继承的成员

  • 1 个公有变量继承自 QObject
  • 5 个静态公有成员继承自 QWidget
  • 10 个静态公有成员继承自 QObject
  • 1 个受保护的槽函数继承自 QWidget
  • 2 个受保护的变量继承自 QObject
  • 1 protected type inherited from QPaintDevice

详细描述

QTabWidget 提供一组标签页.

一个标签页提供一个标签栏 (参见 QTabBar) 和一个 "页面区域" , 显示标签相关的页面. 默认情况下, 标签栏显示在上方, 也可以使用其他配置 (参见 TabPosition). 每个标签关联不同的页. 页面区域只显示当前页; 其他页被隐藏. 用户点击标签或 按Alt+字母 快捷键切换页面.

使用 QTabWidget 的正常方式如下:

  1. 创建一个 QTabWidget.
  2. 为对话框中每个标签页创建一个 QWidget , 但是不指定父对象.
  3. 使用布局或其他方式将子widget插入页面widget.
  4. 调用 addTab() 或 insertTab() 将页面widget添加到标签页, 并设置合适标签及快捷键.

标签页的位置由 tabPosition定义, 形状由 tabShape定义.

用户点选标签时, 发出信号 currentChanged().

调用 currentIndex()获取当前标签页索引, 调用 currentWidget()获取当前标签页. 调用 widget()获取特定索引的标签页, 调用 indexOf()获取特定widget的索引. 调用 setCurrentWidget() 或 setCurrentIndex() 设置当前标签页.

调用 setTabText() 或 setTabIcon()改变标签的文本和图标. 调用 removeTab()移除标签页.

每个标签可以在任意时间设置开启或禁用状态 (参见 setTabEnabled()). 如果一个标签开启, 标签正常绘制并可被用户点选. 如果禁用, 标签采用不同方式绘制并不支持点选. 注意: 标签禁用, 标签页也能显示, 如禁用索引标签.

标签页是分隔复制对话框的一种好方式. 另一种方式是 QStackedWidget, 提供不同页面导航, 例如, QToolBarQListWidget.

QTabWidget 大部分功能由 QTabBar (顶部, 标签栏) 和 a QStackedWidget (页面区域, 管理页面)提供.

Screenshot of a Windows Vista style tab widgetScreenshot of a Macintosh style tab widgetScreenshot of a Fusion style tab widget
A Windows Vista style tab widget.A Macintosh style tab widget.A Fusion style tab widget.

参见 QTabBar, QStackedWidget, QToolBox, and Tab Dialog Example.

成员类型

enum QTabWidget::TabPosition

This enum type defines where QTabWidget draws the tab row:

ConstantValueDescription
QTabWidget::North0The tabs are drawn above the pages.
QTabWidget::South1The tabs are drawn below the pages.
QTabWidget::West2The tabs are drawn to the left of the pages.
QTabWidget::East3The tabs are drawn to the right of the pages.

enum QTabWidget::TabShape

This enum type defines the shape of the tabs:

ConstantValueDescription
QTabWidget::Rounded0The tabs are drawn with a rounded look. This is the default shape.
QTabWidget::Triangular1The tabs are drawn with a triangular look.

属性

count : const int

This property holds the number of tabs in the tab bar

By default, this property contains a value of 0.

访问函数:

int count() const

currentIndex : int

This property holds the index position of the current tab page

The current index is -1 if there is no current widget.

By default, this property contains a value of -1 because there are initially no tabs in the widget.

访问函数:

int currentIndex() const
void setCurrentIndex(int index)

Notifier signal:

void currentChanged(int index)

documentMode : bool

This property holds whether or not the tab widget is rendered in a mode suitable for document pages. This is the same as document mode on macOS.

When this property is set the tab widget frame is not rendered. This mode is useful for showing document-type pages where the page covers most of the tab widget area.

This property was introduced in Qt 4.5.

访问函数:

bool documentMode() const
void setDocumentMode(bool set)

参见 elideMode, QTabBar::documentMode, QTabBar::usesScrollButtons, and QStyle::SH_TabBar_PreferNoArrows.

elideMode : Qt::TextElideMode

how to elide text in the tab bar

This property controls how items are elided when there is not enough space to show them for a given tab bar size.

By default the value is style dependant.

This property was introduced in Qt 4.2.

访问函数:

Qt::TextElideMode elideMode() const
void setElideMode(Qt::TextElideMode)

参见 QTabBar::elideMode, usesScrollButtons, and QStyle::SH_TabBar_ElideMode.

iconSize : QSize

This property holds the size for icons in the tab bar

The default value is style-dependent. This is the maximum size that the icons will have. Icons are not scaled up if they are of smaller size.

This property was introduced in Qt 4.2.

访问函数:

QSize iconSize() const
void setIconSize(const QSize &size)

参见 QTabBar::iconSize.

movable : bool

This property holds whether the user can move the tabs within the tabbar area.

By default, this property is false;

This property was introduced in Qt 4.5.

访问函数:

bool isMovable() const
void setMovable(bool movable)

tabBarAutoHide : bool

If true, the tab bar is automatically hidden when it contains less than 2 tabs.

By default, this property is false.

This property was introduced in Qt 5.4.

访问函数:

bool tabBarAutoHide() const
void setTabBarAutoHide(bool enabled)

参见 QWidget::visible.

tabPosition : TabPosition

This property holds the position of the tabs in this tab widget

Possible values for this property are described by the TabPosition enum.

By default, this property is set to North.

访问函数:

TabPosition tabPosition() const
void setTabPosition(TabPosition)

参见 TabPosition.

tabShape : TabShape

This property holds the shape of the tabs in this tab widget

Possible values for this property are QTabWidget::Rounded (default) or QTabWidget::Triangular.

访问函数:

TabShape tabShape() const
void setTabShape(TabShape s)

参见 TabShape.

tabsClosable : bool

This property holds whether close buttons are automatically added to each tab.

This property was introduced in Qt 4.5.

访问函数:

bool tabsClosable() const
void setTabsClosable(bool closeable)

参见 QTabBar::tabsClosable().

usesScrollButtons : bool

This property holds whether or not a tab bar should use buttons to scroll tabs when it has many tabs.

When there are too many tabs in a tab bar for its size, the tab bar can either choose to expand its size or to add buttons that allow you to scroll through the tabs.

By default the value is style dependant.

This property was introduced in Qt 4.2.

访问函数:

bool usesScrollButtons() const
void setUsesScrollButtons(bool useButtons)

参见 elideMode, QTabBar::usesScrollButtons, and QStyle::SH_TabBar_PreferNoArrows.

成员函数

QTabWidget::QTabWidget(QWidget *parent = Q_NULLPTR)

Constructs a tabbed widget with parent parent.

QTabWidget::~QTabWidget()

Destroys the tabbed widget.

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

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.

参见 insertTab().

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

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.

[virtual protected] void QTabWidget::changeEvent(QEvent *ev)

重新实现 QWidget::changeEvent().

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

QWidget *QTabWidget::cornerWidget(Qt::Corner corner = Qt::TopRightCorner) const

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

参见 setCornerWidget().

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

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)

Note: Notifier signal for property currentIndex.

参见 currentWidget() and currentIndex.

QWidget *QTabWidget::currentWidget() const

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

参见 currentIndex() and setCurrentWidget().

[virtual protected] bool QTabWidget::event(QEvent *ev)

重新实现 QObject::event().

[virtual] bool QTabWidget::hasHeightForWidth() const

重新实现 QWidget::hasHeightForWidth().

[virtual] int QTabWidget::heightForWidth(int width) const

重新实现 QWidget::heightForWidth().

int QTabWidget::indexOf(QWidget *w) const

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

[protected] void QTabWidget::initStyleOption(QStyleOptionTabWidgetFrame *option) const

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.

参见 QStyleOption::initFrom() and QTabBar::initStyleOption().

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

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.

参见 addTab().

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

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.

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

bool QTabWidget::isTabEnabled(int index) const

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

参见 setTabEnabled() and QWidget::isEnabled().

[virtual protected] void QTabWidget::keyPressEvent(QKeyEvent *e)

重新实现 QWidget::keyPressEvent().

[virtual] QSize QTabWidget::minimumSizeHint() const

重新实现 QWidget::minimumSizeHint().

Returns a suitable minimum size for the tab widget.

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

重新实现 QWidget::paintEvent().

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

void QTabWidget::removeTab(int index)

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

参见 addTab() and insertTab().

[virtual protected] void QTabWidget::resizeEvent(QResizeEvent *e)

重新实现 QWidget::resizeEvent().

void QTabWidget::setCornerWidget(QWidget *widget, Qt::Corner corner = Qt::TopRightCorner)

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.

参见 cornerWidget() and setTabPosition().

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

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

参见 addTab(), setCurrentIndex(), and currentWidget().

[protected] void QTabWidget::setTabBar(QTabBar *tb)

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.

参见 tabBar().

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

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.

参见 isTabEnabled() and QWidget::setEnabled().

void QTabWidget::setTabIcon(int index, const QIcon &icon)

This is an overloaded function.

Sets the icon for the tab at position index.

参见 tabIcon().

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

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

参见 tabText().

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

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

参见 tabToolTip().

void QTabWidget::setTabWhatsThis(int index, const 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.

参见 tabWhatsThis().

[virtual protected] void QTabWidget::showEvent(QShowEvent *)

重新实现 QWidget::showEvent().

[virtual] QSize QTabWidget::sizeHint() const

重新实现 QWidget::sizeHint().

QTabBar *QTabWidget::tabBar() const

Returns the current QTabBar.

参见 setTabBar().

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

This signal is emitted when user clicks on a tab at an index.

index refers to the tab clicked, or -1 if no tab is under the cursor.

This function was introduced in Qt 5.2.

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

This signal is emitted when the user double clicks on a tab at an index.

index is the index of a clicked tab, or -1 if no tab is under the cursor.

This function was introduced in Qt 5.2.

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

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.

参见 setTabsClosable().

QIcon QTabWidget::tabIcon(int index) const

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

参见 setTabIcon().

[virtual protected] void QTabWidget::tabInserted(int index)

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

参见 tabRemoved().

[virtual protected] void QTabWidget::tabRemoved(int index)

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

参见 tabInserted().

QString QTabWidget::tabText(int index) const

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

参见 setTabText().

QString QTabWidget::tabToolTip(int index) const

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

参见 setTabToolTip().

QString QTabWidget::tabWhatsThis(int index) const

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.

参见 setTabWhatsThis().

QWidget *QTabWidget::widget(int index) const

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