文章目录
QTabBar Class
QTabBar类提供了一个选项卡栏,例如用于选项卡式对话框。
Header | #include < QTabBar > |
---|---|
qmake | QT += widgets |
Inherits | QWidget |
Inherited By |
详细说明
QTabBar使用起来很简单;它使用一个预定义的形状绘制选项卡,并在选择选项卡时发出信号。它可以细分为定制的外观和感觉。Qt还提供了一个现成的QTabWidget。
每个选项卡都有tabText(), tabIcon(), tabToolTip(), tabWhatsThis()和tabData()。选项卡的属性可以通过setTabText(), setTabIcon(), setTabToolTip(), setTabWhatsThis和setTabData()来改变。每个选项卡都可以使用setTabEnabled()单独启用或禁用。
每个选项卡都可以用不同的颜色显示文本。标签的当前文本颜色可以通过tabTextColor()函数找到。使用setTabTextColor()设置特定标签的文本颜色。
使用addTab()添加选项卡,或使用insertTab()在特定位置插入选项卡。选项卡的总数由count()给出。可以使用removeTab()从选项卡栏中删除选项卡。结合removeTab()和insertTab()可以将选项卡移动到不同的位置。
shape属性定义了选项卡的外观。形状的选择取决于个人喜好,尽管选项卡对话框(用于偏好或类似的选项卡对话框)总是使用四舍五入。除了对话框之外,windows中的选项卡控件几乎总是使用向南四角或向南三角。许多电子表格和其他选项卡控件,在所有的页面本质上是相似的使用三角南,而RoundedSouth主要是在页面不同的时候使用(例如一个多页面工具面板)。QTabBar的默认值是RoundedNorth。
QTabBar API中最重要的部分是currentChanged()信号。当当前选项卡改变时(即使是在启动时,当前选项卡从’none’改变时)就会触发此操作。还有一个槽,setCurrentIndex(),可以使用它以编程方式选择一个选项卡。函数currentIndex()返回当前选项卡的索引,count保存选项卡的数量。
QTabBar以QAbstractButton的方式创建自动助记键;例如,如果一个选项卡的标签是“&Graphics”,Alt+G将成为切换到该选项卡的快捷键。
下面的虚拟函数可能需要重新实现,以便定制外观或在每个选项卡中存储额外的数据:
- tabSizeHint()计算选项卡的大小。
- tabInserted()通知添加了一个新标签。
- tabRemoved()通知标签被删除。
- tabLayoutChange()通知标签已经重新布局。
- paintEvent()绘制所有选项卡。
对于子类,您可能还需要tabRect()函数,它返回单个选项卡的视觉几何图形。
以Fusion小部件样式显示的选项卡栏。 | 一个截短的选项卡栏显示在Fusion小部件风格中。 |
公共类型
enum ButtonPosition
此枚举类型列出小部件在选项卡上的位置。
Constant | Value | Description |
---|---|---|
QTabBar::LeftSide | 0 | 标签的左侧。 |
QTabBar::RightSide | 1 | 标签的右侧。 |
enum SelectionBehavior
此枚举类型列出删除选项卡时QTabBar的行为,并且要删除的选项卡也是当前选项卡。
Constant | Value | Description |
---|---|---|
QTabBar::SelectLeftTab | 0 | 选择要删除的选项卡左侧的选项卡。 |
QTabBar::SelectRightTab | 1 | 选择要删除的选项卡右侧的选项卡。 |
QTabBar::SelectPreviousTab | 2 | 选择先前选择的选项卡。 |
enum Shape
此枚举类型列出QTabBar支持的内置形状。将这些视为提示,因为某些样式可能无法呈现某些形状。然而,这个职位应该得到尊重。
Constant | Value | Description |
---|---|---|
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。Access functions:
- bool autoHide() const
- void setAutoHide(bool hide)
-
changeCurrentOnDrag: bool 如果为true,则在选项卡栏上拖动时,当前选项卡将自动更改
注意:您还应该将acceptDrops属性设置为true以使此功能正常工作。
默认情况下,此属性为false。Access functions:
- bool changeCurrentOnDrag() const
- void setChangeCurrentOnDrag(bool change)
-
count: const int 选项卡栏中的选项卡数
Access functions:
- int count() const
-
currentIndex: int 选项卡栏的可见的选项卡的索引
如果没有当前选项卡,则当前索引为-1。Access functions:
- int currentIndex() const
- void setCurrentIndex(int index)
Notifier signal:
- void currentChanged(int index)
-
documentMode: bool 选项卡栏是否以适合主窗口的模式呈现
此属性用于提示样式以不同的方式绘制选项卡,这些方式通常会在选项卡小部件中查看。在macOS上,这看起来类似于Safari或Sierra的标签打开终端.Access functions:
- bool documentMode() const
- void setDocumentMode(bool set)
-
drawBase: bool 定义选项卡栏是否应绘制其底部
如果为true,那么QTabBar将绘制与覆盖样式相关的基础。否则仅绘制选项卡。Access functions:
- bool drawBase() const
- void setDrawBase(bool drawTheBase)
-
elideMode: Qt::TextElideMode 对于给定的选项卡栏大小,没有足够的空间显示项时,此属性控制如何省略项。
默认情况下,该值取决于样式。Access functions:
- Qt::TextElideMode elideMode() const
- void setElideMode(Qt::TextElideMode mode)
enum Qt::TextElideMode
此枚举指定在显示不适合的文本时省略号应出现的位置:
Constant Value Description Qt::ElideLeft 0 省略号应该出现在文本的开头。 Qt::ElideRight 1 省略号应该出现在文本的末尾。 Qt::ElideMiddle 2 省略号应该出现在文本的中间。 Qt::ElideNone 3 省略号不应出现在文本中。 -
expanding: bool 当expanding为真时,QTabBar将展开选项卡以使用空白
默认情况下,该值为true。Access functions:
- bool expanding() const
- void setExpanding(bool enabled)
-
iconSize: QSize 选项卡栏中图标的大小
默认值取决于样式。iconSize是最大大小;较小的图标不会按比例放大。Access functions:
- QSize iconSize() const
- void setIconSize(const QSize &size)
-
movable: bool 用户是否可以在选项卡栏区域内移动选项卡
默认情况下,此属性为false;Access functions:
- bool isMovable() const
- void setMovable(bool movable)
-
selectionBehaviorOnRemove: SelectionBehavior 删除当前选项卡后,如何设置当前选项卡
如果删除的选项卡也是当前选项卡,则调用removeTab时应将哪个选项卡设置为当前选项卡。默认情况下,该值为SelectRightTab。
Access functions:
- QTabBar::SelectionBehavior selectionBehaviorOnRemove() const
- void setSelectionBehaviorOnRemove(QTabBar::SelectionBehavior behavior)
-
shape: Shape 选项卡栏中选项卡的形状
此属性的可能值由形状枚举描述。Access functions:
- QTabBar::Shape shape() const
- void setShape(QTabBar::Shape shape)
-
tabsClosable: bool 选项卡栏是否应在每个选项卡上放置关闭按钮
当tabsClosable设置为true时,根据样式的不同,选项卡的左侧或右侧会出现一个关闭按钮。当点击按钮时,tabCloseRequested将发出信号。
默认情况下,该值为false。Access functions:
- bool tabsClosable() const
- void setTabsClosable(bool closable)
-
usesScrollButtons: bool 当选项卡栏有许多选项卡时,此属性决定是否应使用按钮滚动选项卡
当选项卡栏中的选项卡太多而无法容纳其大小时,选项卡栏可以选择扩展其大小,也可以添加允许您在选项卡间滚动的按钮。默认情况下,该值取决于样式。
Access functions:
- bool usesScrollButtons() const
- void setUsesScrollButtons(bool useButtons)
公共函数
构造和析构
- QTabBar(QWidget *parent = nullptr)
- virtual ~QTabBar()
属性相关
- bool autoHide() const
- void setAutoHide(bool hide)
- bool changeCurrentOnDrag() const
- void setChangeCurrentOnDrag(bool change)
- int count() const
- int currentIndex() const
- void setCurrentIndex(int index)
- bool documentMode() const
- void setDocumentMode(bool set)
- bool drawBase() const
- void setDrawBase(bool drawTheBase)
- Qt::TextElideMode elideMode() const
- void setElideMode(Qt::TextElideMode mode)
- bool expanding() const
- void setExpanding(bool enabled)
- QSize iconSize() const
- void setIconSize(const QSize &size)
- bool isMovable() const
- void setMovable(bool movable)
- QTabBar::SelectionBehavior selectionBehaviorOnRemove() const
- void setSelectionBehaviorOnRemove(QTabBar::SelectionBehavior behavior)
- QTabBar::Shape shape() const
- void setShape(QTabBar::Shape shape)
- bool tabsClosable() const
- void setTabsClosable(bool closable)
- bool usesScrollButtons() const
- void setUsesScrollButtons(bool useButtons)
修改选项卡
增删移查选项卡
- int addTab(const QString &text)
- int addTab(const QIcon &icon, const QString &text)
- int insertTab(int index, const QString &text)
- int insertTab(int index, const QIcon &icon, const QString &text)
- void moveTab(int from, int to)
- void removeTab(int index)
- int tabAt(const QPoint &position) const
- QRect tabRect(int index) const
设置选项卡
- QWidget * tabButton(int index, QTabBar::ButtonPosition position) const
- void setTabButton(int index, QTabBar::ButtonPosition position, QWidget *widget)
- QVariant tabData(int index) const
- void setTabData(int index, const QVariant &data)
- bool isTabEnabled(int index) const
- void setTabEnabled(int index, bool enabled)
- QIcon tabIcon(int index) const
- void setTabIcon(int index, const QIcon &icon)
- QString tabText(int index) const
- void setTabText(int index, const QString &text)
- QColor tabTextColor(int index) const
- void setTabTextColor(int index, const QColor &color)
- bool isTabVisible(int index) const
- void setTabVisible(int index, bool visible)
- QString tabToolTip(int index) const
- void setTabToolTip(int index, const QString &tip)
- QString tabWhatsThis(int index) const
- void setTabWhatsThis(int index, const QString &text)
重写的公共函数
- 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
相关演示代码
#include <QtWidgets>
#define WidgetType QTabBar
int i=0,j=0;
QList<WidgetType *> widgetList;
QList<QLabel *> labelList;
QCommonStyle cs;
void addFrame(QGridLayout *mainLayout,QWidget *parent){
WidgetType *widget = new WidgetType(parent);
widgetList.append (widget);
QLabel *label = new QLabel(parent);
labelList.append (label);
auto *frame = new QFrame(parent);
frame->setFrameStyle (QFrame::Panel | QFrame::Plain);
auto *vBox = new QVBoxLayout(frame);
vBox->addWidget (label,Qt::AlignCenter);
vBox->addWidget (widget,Qt::AlignCenter);
mainLayout->addWidget (frame,i,j);
j++;
}
void setLabelText(int i,QString text){
if(i >= labelList.count ()){
return;
}
QLabel *label = labelList.at (i);
label->setText (text);
}
WidgetType* getWidget(int i){
if(i >= widgetList.count ()){
qDebug() << __LINE__ <<"行 getWidget(int i) 函数 变量"<< i << "超出范围";
i = 0; // 超出范围默认使用 0
}
return widgetList.at (i);
}
int main(int argc, char *argv[])
{
// QApplication::setStyle (QStyleFactory::create ("fusion")); //windowsvista macintosh fusion
QApplication app(argc,argv);
QMetaObject mo = WidgetType::staticMetaObject;
app.setApplicationName (mo.className ());
QDialog w;
auto *mainLayout = new QGridLayout(&w);
addFrame (mainLayout,&w);
addFrame (mainLayout,&w);
getWidget (0)->addTab (cs.standardIcon( (QStyle::StandardPixmap) 0),"General" );
getWidget (0)->addTab (cs.standardIcon( (QStyle::StandardPixmap) 1),"Permissions" );
getWidget (0)->setShape (QTabBar::RoundedNorth);
getWidget (0)->setUsesScrollButtons (false);
setLabelText (0,"setTabsClosable (false)");
getWidget (1)->addTab (cs.standardIcon( (QStyle::StandardPixmap) 0),"General" );
getWidget (1)->addTab (cs.standardIcon( (QStyle::StandardPixmap) 1),"Permissions" );
getWidget (1)->setShape (QTabBar::RoundedNorth);
getWidget (1)->setUsesScrollButtons (true);
setLabelText (1,"setTabsClosable (true)");
// w.resize (240,50);
w.show ();
app.exec();
return 0;
}