QTabBar 類

QTabBar 類提供選項卡欄,如:為用於選項卡式對話框。 更多...

頭: #include <QTabBar>
qmake: QT += widgets
繼承: QWidget

公共類型

enum ButtonPosition { LeftSide, RightSide }
enum SelectionBehavior { SelectLeftTab, SelectRightTab, SelectPreviousTab }
enum Shape { RoundedNorth, RoundedSouth, RoundedWest, RoundedEast, TriangularNorth, …, TriangularEast }

特性

公共函數

QTabBar (QWidget * parent = nullptr)
virtual ~QTabBar ()
QString accessibleTabName (int index ) const
int addTab (const QString & text )
int addTab (const QIcon & icon , const QString & text )
bool autoHide () const
bool changeCurrentOnDrag () const
int count () const
int currentIndex () const
bool documentMode () const
bool drawBase () const
Qt::TextElideMode elideMode () const
bool expanding () const
QSize iconSize () const
int insertTab (int index , const QString & text )
int insertTab (int index , const QIcon & icon , const QString & text )
bool isMovable () const
bool isTabEnabled (int index ) const
bool isTabVisible (int index ) const
void moveTab (int from , int to )
void removeTab (int index )
QTabBar::SelectionBehavior selectionBehaviorOnRemove () const
void setAccessibleTabName (int index , const QString & name )
void setAutoHide (bool hide )
void setChangeCurrentOnDrag (bool change )
void setDocumentMode (bool set )
void setDrawBase (bool drawTheBase )
void setElideMode (Qt::TextElideMode mode )
void setExpanding (bool enabled )
void setIconSize (const QSize & size )
void setMovable (bool movable )
void setSelectionBehaviorOnRemove (QTabBar::SelectionBehavior behavior )
void setShape (QTabBar::Shape shape )
void setTabButton (int index , QTabBar::ButtonPosition position , QWidget * widget )
void setTabData (int index , const QVariant & data )
void setTabEnabled (int index , bool enabled )
void setTabIcon (int index , const QIcon & icon )
void setTabText (int index , const QString & text )
void setTabTextColor (int index , const QColor & color )
void setTabToolTip (int index , const QString & tip )
void setTabVisible (int index , bool visible )
void setTabWhatsThis (int index , const QString & text )
void setTabsClosable (bool closable )
void setUsesScrollButtons (bool useButtons )
QTabBar::Shape shape () const
int tabAt (const QPoint & position ) const
QWidget * tabButton (int index , QTabBar::ButtonPosition position ) const
QVariant tabData (int index ) const
QIcon tabIcon (int index ) const
QRect tabRect (int index ) const
QString tabText (int index ) const
QColor tabTextColor (int index ) const
QString tabToolTip (int index ) const
QString tabWhatsThis (int index ) const
bool tabsClosable () const
bool usesScrollButtons () const

重實現公共函數

virtual QSize minimumSizeHint () const override
virtual QSize sizeHint () const override

公共槽

void setCurrentIndex (int index )

信號

void currentChanged (int index )
void tabBarClicked (int index )
void tabBarDoubleClicked (int index )
void tabCloseRequested (int index )
void tabMoved (int from , int to )

保護函數

void initStyleOption (QStyleOptionTab * option , int tabIndex ) const
virtual QSize minimumTabSizeHint (int index ) const
virtual void tabInserted (int index )
virtual void tabLayoutChange ()
virtual void tabRemoved (int index )
virtual QSize tabSizeHint (int index ) const

重實現保護函數

virtual void changeEvent (QEvent * event ) override
virtual bool event (QEvent * event ) override
virtual void hideEvent ( QHideEvent * ) override
virtual void keyPressEvent (QKeyEvent * event ) override
virtual void mouseMoveEvent (QMouseEvent * event ) override
virtual void mousePressEvent (QMouseEvent * event ) override
virtual void mouseReleaseEvent (QMouseEvent * event ) override
virtual void paintEvent ( QPaintEvent * ) override
virtual void resizeEvent ( QResizeEvent * ) override
virtual void showEvent ( QShowEvent * ) override
virtual void timerEvent (QTimerEvent * event ) override
virtual void wheelEvent (QWheelEvent * event ) override

詳細描述

QTabBar 易於使用。它繪製選項卡是使用某一預定義 shapes ,並發射信號,當選項卡被選中時。它可以被子類化以訂製外觀和感覺。Qt 還提供現成 QTabWidget .

每個選項卡有 tabText (),可選 tabIcon (),可選 tabToolTip (),可選 tabWhatsThis () 和可選 tabData ()。可以改變選項卡屬性采用 setTabText (), setTabIcon (), setTabToolTip (), setTabWhatsThis and setTabData ()。每個選項卡可以被單獨啓用或禁用采用 setTabEnabled ().

每個選項卡可以按截然不同顔色顯示文本。可以找到當前選項卡的文本顔色采用 tabTextColor () 函數。設置用於特定選項卡的文本顔色采用 setTabTextColor ().

添加選項卡使用 addTab (),或插入在特定位置使用 insertTab ()。選項卡總數的給齣通過 count ()。可以從選項卡欄移除選項卡采用 removeTab ()。組閤 removeTab () 和 insertTab () 允許將選項卡移至不同位置。

The shape 特性定義選項卡的外觀。形狀的選擇是品味問題,盡管選項卡對話框 (對於首選項和類似的) 始終不變使用 RoundedNorth 。窗口 (除對話框外) 中的選項卡控件幾乎總是使用 RoundedSouth or TriangularSouth 。許多電子錶格和其它所有頁麵基本類似的選項卡控件使用 TriangularSouth ,而 RoundedSouth 主要在頁麵不同時使用 (如:多頁麵工具調色闆)。默認在 QTabBar 是 RoundedNorth .

最重要部分的 QTabBar API 是 currentChanged () 信號。這被發射每當當前選項卡改變時 (即使在啓動時,當當前選項卡從 none 改變時)。還有槽, setCurrentIndex () 可以用於以編程方式選擇選項卡。函數 currentIndex () 返迴當前選項卡的索引, count 保存選項卡的數量。

QTabBar 創建自動助記符鍵按方式 QAbstractButton ;如:若選項卡的標簽為 &Graphics,Alt+G 將變為切換到該選項卡的快捷鍵。

可能需要重實現以下虛函數,為每個選項卡訂製外觀和感覺或存儲額外數據:

對於子類,可能還需要 tabRect () 函數返迴單個選項卡的視覺幾何體。

Screenshot of a Fusion style tab bar 選項卡欄展示在 Fusion 小部件風格 .
Screenshot of a truncated Fusion tab bar 以 Fusion 小部件樣式展示截取選項卡欄。

另請參閱 QTabWidget .

成員類型文檔編製

enum QTabBar:: ButtonPosition

此枚舉類型列錶 Widget 在選項卡上的位置。

常量 描述
QTabBar::LeftSide 0 選項卡的左側。
QTabBar::RightSide 1 選項卡的右側。

該枚舉在 Qt 4.5 引入或被修改。

enum QTabBar:: SelectionBehavior

此枚舉類型列錶行為在 QTabBar 當選項卡被移除且被移除選項卡也是當前選項卡時。

常量 描述
QTabBar::SelectLeftTab 0 選擇被移除選項卡左側的選項卡。
QTabBar::SelectRightTab 1 選擇被移除選項卡右側的選項卡。
QTabBar::SelectPreviousTab 2 選擇先前選中的選項卡。

該枚舉在 Qt 4.5 引入或被修改。

enum QTabBar:: Shape

此枚舉類型列齣的內置形狀支持通過 QTabBar 。視這些為提示,因為某些樣式可能無法渲染某些形狀。不管怎樣,位置應該被承兌。

常量 描述
QTabBar::RoundedNorth 0 在頁麵上方的正常圓角外觀
QTabBar::RoundedSouth 1 在頁麵下方的正常圓角外觀
QTabBar::RoundedWest 2 在頁麵左側的正常圓角外觀
QTabBar::RoundedEast 3 在頁麵右側的正常圓角外觀
QTabBar::TriangularNorth 4 在頁麵上方的三角形選項卡。
QTabBar::TriangularSouth 5 例如,類似於 Excel 電子錶格使用的那些三角形選項卡
QTabBar::TriangularWest 6 在頁麵左側的三角形選項卡。
QTabBar::TriangularEast 7 在頁麵右側的三角形選項卡。

特性文檔編製

autoHide : bool

若為 true,自動隱藏選項卡欄,當包含少於 2 個選項卡時。

默認情況下,此特性為 false。

該特性在 Qt 5.4 引入。

訪問函數:

bool autoHide () const
void setAutoHide (bool hide )

另請參閱 QWidget::visible .

changeCurrentOnDrag : bool

若為 true,當前選項卡會自動改變,當在選項卡欄拖拽時。

注意: 還應設置 acceptDrops 特性為 true 以使此特徵工作。

默認情況下,此特性為 false。

該特性在 Qt 5.4 引入。

訪問函數:

bool changeCurrentOnDrag () const
void setChangeCurrentOnDrag (bool change )

count : const int

此特性保持選項卡欄的選項卡數

訪問函數:

int count () const

currentIndex : int

此特性保持選項卡欄的可見選項卡索引

當前索引為 -1,若沒有當前選項卡。

訪問函數:

int currentIndex () const
void setCurrentIndex (int index )

通知程序信號:

void currentChanged (int index )

documentMode : bool

此特性保持選項卡欄是否以適於主窗口的模式進行渲染。

此特性通常用作它們在選項卡 Widget 中按不同外觀方式繪製選項卡的樣式提示。在 macOS,外觀將類似 Safari 或 Sierra 的 Terminal.app 選項卡。

該特性在 Qt 4.5 引入。

訪問函數:

bool documentMode () const
void setDocumentMode (bool set )

另請參閱 QTabWidget::documentMode .

drawBase : bool

定義選項卡欄是否應該繪製其基。

若 true 則 QTabBar 繪製樣式重疊關係基。否則隻繪製選項卡。

訪問函數:

bool drawBase () const
void setDrawBase (bool drawTheBase )

另請參閱 QStyle::pixelMetric (), QStyle::PM_TabBarBaseOverlap ,和 QStyleOptionTabBarBase .

elideMode : Qt::TextElideMode

如何省略標簽欄文本

此特性控製如何省略項,當沒有足夠空間展示它們 (對於給定選項卡欄尺寸) 時。

默認值從屬樣式。

該特性在 Qt 4.2 引入。

訪問函數:

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

另請參閱 QTabWidget::elideMode , usesScrollButtons ,和 QStyle::SH_TabBar_ElideMode .

expanding : bool

當展開為 true QTabBar 將展開選項卡以使用空白空間。

默認情況下,值為 true。

該特性在 Qt 4.5 引入。

訪問函數:

bool expanding () const
void setExpanding (bool enabled )

另請參閱 QTabWidget::documentMode .

iconSize : QSize

此特性保持選項卡欄的圖標尺寸

默認值從屬樣式。 iconSize 是最大尺寸;不會按比例放大較小圖標。

該特性在 Qt 4.1 引入。

訪問函數:

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

另請參閱 QTabWidget::iconSize .

movable : bool

此特性保持用戶是否可以在選項卡欄區域內,移動選項卡。

默認情況下,此特性為 false ;

該特性在 Qt 4.5 引入。

訪問函數:

bool isMovable () const
void setMovable (bool movable )

selectionBehaviorOnRemove : SelectionBehavior

什麼選項卡應被設為當前,在 removeTab 被調用,若被移除選項卡也是當前選項卡。

默認情況下,值為 SelectRightTab .

該特性在 Qt 4.5 引入。

訪問函數:

QTabBar::SelectionBehavior selectionBehaviorOnRemove () const
void setSelectionBehaviorOnRemove (QTabBar::SelectionBehavior behavior )

另請參閱 removeTab ().

shape : Shape

此特性保持選項卡欄的選項卡形狀

此特性的可能值,由形狀枚舉描述。

訪問函數:

QTabBar::Shape shape () const
void setShape (QTabBar::Shape shape )

tabsClosable : bool

此特性保持選項卡欄,是否應在各選項卡中放置關閉按鈕

當 tabsClosable 被設為 true 時,關閉按鈕將齣現在選項卡的左側或右側 (從屬風格)。當按鈕被點擊,選項卡信號 tabCloseRequested 會被發射。

默認情況下,值為 false。

該特性在 Qt 4.5 引入。

訪問函數:

bool tabsClosable () const
void setTabsClosable (bool closable )

另請參閱 setTabButton () 和 tabRemoved ().

usesScrollButtons : bool

此特性保持選項卡欄是否應使用捲動按鈕,當包含多個選項卡時。

當選項卡欄選項卡太多時 (對於其大小),選項卡欄可以選擇展開其大小,或添加按鈕以允許捲動選項卡。

默認值從屬樣式。

該特性在 Qt 4.2 引入。

訪問函數:

bool usesScrollButtons () const
void setUsesScrollButtons (bool useButtons )

另請參閱 elideMode , QTabWidget::usesScrollButtons ,和 QStyle::SH_TabBar_PreferNoArrows .

成員函數文檔編製

QTabBar:: QTabBar ( QWidget * parent = nullptr)

創建新的選項卡欄采用給定 parent .

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

此信號被發射當選項卡欄的當前選項卡改變時。新的當前具有給定 index ,或 -1 若沒有新的 (例如:若沒有選項卡在 QTabBar )

注意: 通知程序信號對於特性 currentIndex .

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

此信號會被發射,當用戶點擊選項卡在 index .

index 是點擊選項卡索引,或 -1 若光標下沒有選項卡。

該函數在 Qt 5.2 引入。

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

此信號被發射當用戶雙擊選項卡在 index .

index 引用選項卡點擊,或 -1 若光標下沒有選項卡。

該函數在 Qt 5.2 引入。

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

此信號被發射當點擊選項卡關閉按鈕時。 index 是應被移除的索引。

該函數在 Qt 4.5 引入。

另請參閱 setTabsClosable ().

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

此信號被發射當選項卡已移動選項卡在索引位置 from 到索引位置 to .

注意: QTabWidget 會自動移動頁麵,當此信號從其選項卡欄被發射時。

該函數在 Qt 4.5 引入。

另請參閱 moveTab ().

[虛擬] QTabBar:: ~QTabBar ()

銷毀選項卡欄。

QString QTabBar:: accessibleTabName ( int index ) const

返迴 accessibleName 對於選項卡在位置 index ,或空字符串若 index 超齣範圍。

另請參閱 setAccessibleTabName ().

int QTabBar:: addTab (const QString & text )

添加新選項卡采用文本 text 。返迴新選項卡的索引。

int QTabBar:: addTab (const QIcon & icon , const QString & text )

這是重載函數。

添加新選項卡采用圖標 icon 和文本 text 。返迴新選項卡的索引。

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

重實現: QWidget::changeEvent (QEvent *event).

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

重實現: QWidget::event (QEvent *event).

[override virtual protected] void QTabBar:: hideEvent ( QHideEvent * )

重實現: QWidget::hideEvent (QHideEvent *event).

[protected] void QTabBar:: initStyleOption ( QStyleOptionTab * option , int tabIndex ) const

初始化 option 采用值從選項卡在 tabIndex 。此方法對子類是有用的,當需要 QStyleOptionTab ,但不希望自己填充所有信息。

另請參閱 QStyleOption::initFrom () 和 QTabWidget::initStyleOption ().

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

插入新選項卡采用文本 text 在位置 index 。若 index 超齣範圍,追加新選項卡。返迴新選項卡的索引。

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

這是重載函數。

插入新選項卡采用圖標 icon 和文本 text 在位置 index 。若 index 超齣範圍,追加新選項卡。返迴新選項卡的索引。

QTabBar 為空在調用此函數之前,插入選項卡變為當前選項卡。

在 <= 當前索引的索引處插入新選項卡將遞增當前索引,但保持當前選項卡。

bool QTabBar:: isTabEnabled ( int index ) const

返迴 true 若選項卡在位置 index 被啓用;否則返迴 false .

bool QTabBar:: isTabVisible ( int index ) const

返迴 true 若選項卡在位置 index 可見;否則返迴 false。

該函數在 Qt 5.15 引入。

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

重實現: QWidget::keyPressEvent (QKeyEvent *event).

[override virtual] QSize QTabBar:: minimumSizeHint () const

重實現訪問函數為特性: QWidget::minimumSizeHint .

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

返迴最小選項卡大小提示,對於選項卡在位置 index .

該函數在 Qt 5.0 引入。

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

重實現: QWidget::mouseMoveEvent (QMouseEvent *event).

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

重實現: QWidget::mousePressEvent (QMouseEvent *event).

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

重實現: QWidget::mouseReleaseEvent (QMouseEvent *event).

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

移動項按索引位置 from 到索引位置 to .

該函數在 Qt 4.5 引入。

另請參閱 tabMoved () 和 tabLayoutChange ().

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

重實現: QWidget::paintEvent (QPaintEvent *event).

void QTabBar:: removeTab ( int index )

移除選項卡,在位置 index .

另請參閱 SelectionBehavior .

[override virtual protected] void QTabBar:: resizeEvent ( QResizeEvent * )

重實現: QWidget::resizeEvent (QResizeEvent *event).

void QTabBar:: setAccessibleTabName ( int index , const QString & name )

設置 accessibleName 對於選項卡在位置 index to name .

另請參閱 accessibleTabName ().

void QTabBar:: setTabButton ( int index , QTabBar::ButtonPosition position , QWidget * widget )

設置 widget 在選項卡 index 。Widget 被放置在左側或右側從屬 position .

先前設置的任何 Widget 在 position 被隱藏。

選項卡欄將擁有小部件的所有權,因此,選項卡欄會刪除此處設置的所有小部件當銷毀時,除非單獨重設 Widget 父級在設置一些其它小部件後 (或 nullptr ).

該函數在 Qt 4.5 引入。

另請參閱 tabButton () 和 tabsClosable ().

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

設置選項卡數據在位置 index to data .

另請參閱 tabData ().

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

enabled 為 true 則選項卡在位置 index 被啓用;否則項在位置 index 被禁用。

另請參閱 isTabEnabled ().

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

設置選項卡的圖標,在位置 index to icon .

另請參閱 tabIcon ().

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

設置選項卡的文本,在位置 index to text .

另請參閱 tabText ().

void QTabBar:: setTabTextColor ( int index , const QColor & color )

設置選項卡文本顔色采用給定 index 到指定 color .

若指定的是無效顔色,選項卡將使用 QTabBar 前景角色代替。

另請參閱 tabTextColor ().

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

設置選項卡的工具提示,在位置 index to tip .

另請參閱 tabToolTip ().

void QTabBar:: setTabVisible ( int index , bool visible )

visible 為 true,使選項卡在位置 index 可見,否則使之隱藏。

該函數在 Qt 5.15 引入。

另請參閱 isTabVisible ().

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

設置選項卡的 What's This 幫助文本,在位置 index to text .

該函數在 Qt 4.1 引入。

另請參閱 tabWhatsThis ().

[override virtual protected] void QTabBar:: showEvent ( QShowEvent * )

重實現: QWidget::showEvent (QShowEvent *event).

[override virtual] QSize QTabBar:: sizeHint () const

重實現訪問函數為特性: QWidget::sizeHint .

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

返迴選項卡索引,覆蓋 position 或 -1 若沒有選項卡覆蓋 position ;

該函數在 Qt 4.3 引入。

QWidget *QTabBar:: tabButton ( int index , QTabBar::ButtonPosition position ) const

返迴 Widget 設置選項卡 index and position or nullptr 若未設置。

另請參閱 setTabButton ().

QVariant QTabBar:: tabData ( int index ) const

返迴選項卡數據在位置 index ,或 null 變體若 index 超齣範圍。

另請參閱 setTabData ().

QIcon QTabBar:: tabIcon ( int index ) const

返迴選項卡的圖標,在位置 index ,或 null 圖標若 index 超齣範圍。

另請參閱 setTabIcon ().

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

調用此虛擬處理程序,在添加或插入新選項卡後於位置 index .

另請參閱 tabRemoved ().

[virtual protected] void QTabBar:: tabLayoutChange ()

此虛擬處理程序被調用,每當選項卡布局改變時。

另請參閱 tabRect ().

QRect QTabBar:: tabRect ( int index ) const

返迴選項卡的視覺矩形,在位置 index ,或 null 矩形若 index 被隱藏或超齣範圍。

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

此虛擬處理程序被調用,在移除選項卡後從位置 index .

另請參閱 tabInserted ().

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

返迴選項卡的大小提示按位置 index .

QString QTabBar:: tabText ( int index ) const

返迴選項卡的文本,在位置 index ,或空字符串若 index 超齣範圍。

另請參閱 setTabText ().

QColor QTabBar:: tabTextColor ( int index ) const

返迴選項卡的文本顔色,采用給定 index ,或無效顔色若 index 超齣範圍。

另請參閱 setTabTextColor ().

QString QTabBar:: tabToolTip ( int index ) const

返迴選項卡的工具提示,在位置 index ,或空字符串若 index 超齣範圍。

另請參閱 setTabToolTip ().

QString QTabBar:: tabWhatsThis ( int index ) const

返迴選項卡的 What's This 幫助文本,在位置 index ,或空字符串若 index 超齣範圍。

該函數在 Qt 4.1 引入。

另請參閱 setTabWhatsThis ().

[override virtual protected] void QTabBar:: timerEvent ( QTimerEvent * event )

重實現: QObject::timerEvent (QTimerEvent *event).

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

重實現: QWidget::wheelEvent (QWheelEvent *event).