当前位置:   article > 正文

基本Widgets(32):【类】QTabBar[官翻]

qtabbar

QTabBar Class

QTabBar类提供了一个选项卡栏,例如用于选项卡式对话框。

Header#include < QTabBar >
qmakeQT += widgets
InheritsQWidget
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()函数,它返回单个选项卡的视觉几何图形。

Screenshot of a Fusion style tab barScreenshot of a truncated Fusion tab bar
以Fusion小部件样式显示的选项卡栏。一个截短的选项卡栏显示在Fusion小部件风格中。

公共类型

enum ButtonPosition

此枚举类型列出小部件在选项卡上的位置。

ConstantValueDescription
QTabBar::LeftSide0标签的左侧。
QTabBar::RightSide1标签的右侧。

enum SelectionBehavior

此枚举类型列出删除选项卡时QTabBar的行为,并且要删除的选项卡也是当前选项卡。

ConstantValueDescription
QTabBar::SelectLeftTab0选择要删除的选项卡左侧的选项卡。
QTabBar::SelectRightTab1选择要删除的选项卡右侧的选项卡。
QTabBar::SelectPreviousTab2选择先前选择的选项卡。

enum Shape

此枚举类型列出QTabBar支持的内置形状。将这些视为提示,因为某些样式可能无法呈现某些形状。然而,这个职位应该得到尊重。

ConstantValueDescription
QTabBar::RoundedNorth0页面上方正常的圆形外观
QTabBar::RoundedSouth1页面下方正常的圆形外观
QTabBar::RoundedWest2页面左侧正常的圆形外观
QTabBar::RoundedEast3页面右侧正常的圆形外观
QTabBar::TriangularNorth4页面上方的三角形标签。
QTabBar::TriangularSouth5例如,类似于Excel电子表格中使用的三角形选项卡
QTabBar::TriangularWest6页面左侧的三角形选项卡。
QTabBar::TriangularEast7页面右侧的三角形选项卡。

属性

  1. autoHide: bool 如果为true,则当选项卡栏包含少于2个选项卡时,它将自动隐藏
    默认情况下,此属性为false。

    Access functions:

    • bool autoHide() const
    • void setAutoHide(bool hide)

  2. changeCurrentOnDrag: bool 如果为true,则在选项卡栏上拖动时,当前选项卡将自动更改
    注意:您还应该将acceptDrops属性设置为true以使此功能正常工作。
    默认情况下,此属性为false。

    Access functions:

    • bool changeCurrentOnDrag() const
    • void setChangeCurrentOnDrag(bool change)
  3. count: const int 选项卡栏中的选项卡数

    Access functions:

    • int count() const
  4. currentIndex: int 选项卡栏的可见的选项卡的索引
    如果没有当前选项卡,则当前索引为-1。

    Access functions:

    • int currentIndex() const
    • void setCurrentIndex(int index)

    Notifier signal:

    • void currentChanged(int index)
  5. documentMode: bool 选项卡栏是否以适合主窗口的模式呈现
    此属性用于提示样式以不同的方式绘制选项卡,这些方式通常会在选项卡小部件中查看。在macOS上,这看起来类似于Safari或Sierra的标签打开终端.

    Access functions:

    • bool documentMode() const
    • void setDocumentMode(bool set)
  6. drawBase: bool 定义选项卡栏是否应绘制其底部
    如果为true,那么QTabBar将绘制与覆盖样式相关的基础。否则仅绘制选项卡。

    Access functions:

    • bool drawBase() const
    • void setDrawBase(bool drawTheBase)

  7. elideMode: Qt::TextElideMode 对于给定的选项卡栏大小,没有足够的空间显示项时,此属性控制如何省略项。
    默认情况下,该值取决于样式。

    Access functions:

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

    enum Qt::TextElideMode

    此枚举指定在显示不适合的文本时省略号应出现的位置:

    ConstantValueDescription
    Qt::ElideLeft0省略号应该出现在文本的开头。
    Qt::ElideRight1省略号应该出现在文本的末尾。
    Qt::ElideMiddle2省略号应该出现在文本的中间。
    Qt::ElideNone3省略号不应出现在文本中。

  8. expanding: bool 当expanding为真时,QTabBar将展开选项卡以使用空白
    默认情况下,该值为true。

    Access functions:

    • bool expanding() const
    • void setExpanding(bool enabled)
  9. iconSize: QSize 选项卡栏中图标的大小
    默认值取决于样式。iconSize是最大大小;较小的图标不会按比例放大。

    Access functions:

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

  10. movable: bool 用户是否可以在选项卡栏区域内移动选项卡
    默认情况下,此属性为false;

    Access functions:

    • bool isMovable() const
    • void setMovable(bool movable)

  11. selectionBehaviorOnRemove: SelectionBehavior 删除当前选项卡后,如何设置当前选项卡
    如果删除的选项卡也是当前选项卡,则调用removeTab时应将哪个选项卡设置为当前选项卡。

    默认情况下,该值为SelectRightTab。

    Access functions:

    • QTabBar::SelectionBehavior selectionBehaviorOnRemove() const
    • void setSelectionBehaviorOnRemove(QTabBar::SelectionBehavior behavior)
  12. shape: Shape 选项卡栏中选项卡的形状
    此属性的可能值由形状枚举描述。

    Access functions:

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

  13. tabsClosable: bool 选项卡栏是否应在每个选项卡上放置关闭按钮
    当tabsClosable设置为true时,根据样式的不同,选项卡的左侧或右侧会出现一个关闭按钮。当点击按钮时,tabCloseRequested将发出信号。
    默认情况下,该值为false。

    Access functions:

    • bool tabsClosable() const
    • void setTabsClosable(bool closable)

  14. usesScrollButtons: bool 当选项卡栏有许多选项卡时,此属性决定是否应使用按钮滚动选项卡

    当选项卡栏中的选项卡太多而无法容纳其大小时,选项卡栏可以选择扩展其大小,也可以添加允许您在选项卡间滚动的按钮。默认情况下,该值取决于样式。

    Access functions:

    • bool usesScrollButtons() const
    • void setUsesScrollButtons(bool useButtons)

公共函数

构造和析构

  1. QTabBar(QWidget *parent = nullptr)
  2. virtual ~QTabBar()

属性相关

  1. bool autoHide() const
  2. void setAutoHide(bool hide)
  3. bool changeCurrentOnDrag() const
  4. void setChangeCurrentOnDrag(bool change)
  5. int count() const
  6. int currentIndex() const
  7. void setCurrentIndex(int index)
  8. bool documentMode() const
  9. void setDocumentMode(bool set)
  10. bool drawBase() const
  11. void setDrawBase(bool drawTheBase)
  12. Qt::TextElideMode elideMode() const
  13. void setElideMode(Qt::TextElideMode mode)
  14. bool expanding() const
  15. void setExpanding(bool enabled)
  16. QSize iconSize() const
  17. void setIconSize(const QSize &size)
  18. bool isMovable() const
  19. void setMovable(bool movable)
  20. QTabBar::SelectionBehavior selectionBehaviorOnRemove() const
  21. void setSelectionBehaviorOnRemove(QTabBar::SelectionBehavior behavior)
  22. QTabBar::Shape shape() const
  23. void setShape(QTabBar::Shape shape)
  24. bool tabsClosable() const
  25. void setTabsClosable(bool closable)
  26. bool usesScrollButtons() const
  27. void setUsesScrollButtons(bool useButtons)

修改选项卡

增删移查选项卡

  1. int addTab(const QString &text)
  2. int addTab(const QIcon &icon, const QString &text)
  3. int insertTab(int index, const QString &text)
  4. int insertTab(int index, const QIcon &icon, const QString &text)
  5. void moveTab(int from, int to)
  6. void removeTab(int index)
  7. int tabAt(const QPoint &position) const
  8. QRect tabRect(int index) const

设置选项卡

  1. QWidget * tabButton(int index, QTabBar::ButtonPosition position) const
  2. void setTabButton(int index, QTabBar::ButtonPosition position, QWidget *widget)
  3. QVariant tabData(int index) const
  4. void setTabData(int index, const QVariant &data)
  5. bool isTabEnabled(int index) const
  6. void setTabEnabled(int index, bool enabled)
  7. QIcon tabIcon(int index) const
  8. void setTabIcon(int index, const QIcon &icon)
  9. QString tabText(int index) const
  10. void setTabText(int index, const QString &text)
  11. QColor tabTextColor(int index) const
  12. void setTabTextColor(int index, const QColor &color)
  13. bool isTabVisible(int index) const
  14. void setTabVisible(int index, bool visible)
  15. QString tabToolTip(int index) const
  16. void setTabToolTip(int index, const QString &tip)
  17. QString tabWhatsThis(int index) const
  18. void setTabWhatsThis(int index, const QString &text)

重写的公共函数

  1. virtual QSize minimumSizeHint() const override
  2. virtual QSize sizeHint() const override

公共槽

  1. void setCurrentIndex(int index)

信号

  1. void currentChanged(int index)
  2. void tabBarClicked(int index)
  3. void tabBarDoubleClicked(int index)
  4. void tabCloseRequested(int index)
  5. void tabMoved(int from, int to)

受保护的函数

  1. void initStyleOption(QStyleOptionTab *option, int tabIndex) const
  2. virtual QSize minimumTabSizeHint(int index) const
  3. virtual void tabInserted(int index)
  4. virtual void tabLayoutChange()
  5. virtual void tabRemoved(int index)
  6. virtual QSize tabSizeHint(int index) const

重写的受保护的函数

  1. virtual void changeEvent(QEvent *event) override
  2. virtual bool event(QEvent *event) override
  3. virtual void hideEvent(QHideEvent *) override
  4. virtual void keyPressEvent(QKeyEvent *event) override
  5. virtual void mouseMoveEvent(QMouseEvent *event) override
  6. virtual void mousePressEvent(QMouseEvent *event) override
  7. virtual void mouseReleaseEvent(QMouseEvent *event) override
  8. virtual void paintEvent(QPaintEvent *) override
  9. virtual void resizeEvent(QResizeEvent *) override
  10. virtual void showEvent(QShowEvent *) override
  11. virtual void timerEvent(QTimerEvent *event) override
  12. 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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/384935
推荐阅读
相关标签
  

闽ICP备14008679号