基本Widgets(32):【类】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;
}
  • 7
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 错误信息:"pycharm导入不成功from pyqt5 import qtwidgets importerror: dll load failed: %",这个错误通常是由于缺少相应的动态链接库(dll)文件或者dll文件版本不匹配引起的。 解决这个问题的一种方法是重新安装PyQt5库,并确保选择与所使用的Python版本相对应的库版本。可以通过命令提示符或终端运行以下命令来重新安装PyQt5库: pip uninstall PyQt5 pip install PyQt5 另外,也可以尝试下载并安装Microsoft Visual C++ Redistributable包,以确保系统具有所需的C++运行时库。 如果上述方法都无法解决问题,还可以尝试更新PyQt5库的版本,或者在PyCharm中重新设置Python解释器路径,确保正确指定Python解释器。 总之,导入错误通常是因为缺失或版本不匹配的dll文件,通过重新安装库或更新版本,以及安装相应的C++运行时库,可以解决该问题。 ### 回答2: 这个错误信息意味着在导入pyqt5库的时候发生了问题,并且指出了导入错误的原因是“dll load failed”。这种情况通常发生在某些Windows系统上,可能是因为缺少Qt运行库或者Qt运行库不兼容所致。 要解决这个问题,你可以尝试以下几个步骤: 1. 确保你已经正确安装了pyqt5库。你可以通过运行`pip install pyqt5`来安装最新版本的pyqt5库。 2. 检查你的系统环境变量。确保你的系统环境变量中的路径包含了正确的Qt运行库路径。你可以在Qt安装目录中搜索并找到你系统版本所需的库文件,然后将该路径添加到系统环境变量中。 3. 检查你的Python版本和Qt运行库版本是否兼容。如果你使用的是Python的64位版本,那么请确保你安装的是64位的Qt运行库。 4. 尝试重新安装Qt运行库。你可以在Qt的官方网站上下载适合你系统和Python版本的Qt运行库,并进行重新安装。 5. 尝试使用conda来安装pyqt5库。有时候,使用conda安装库可以避免一些兼容性问题。 如果你尝试了以上方法仍然无法解决问题,那么可能是由于其他的系统配置问题所致。你可以尝试在相关的技术论坛或社区中寻求帮助,或者向PyCharm的开发者反馈该问题,他们可能能够提供更准确的解决方案。 ### 回答3: 当我们遇到 "from pyqt5 import qtwidgets importerror: dll load failed"这个错误时,通常是因为pycharm无法找到所需的dll文件。 解决此问题的步骤如下: 1. 首先,我们需要确保已经正确安装了 PyQt5 模块。可以通过在终端中运行 "pip install pyqt5" 命令来安装。 2. 检查是否存在 dll 文件。在运行pycharm前,请确保系统中有必要的 dll 文件,特别是针对 PyQt5 模块包含的 dll 文件。这些文件通常在 PyQt5 的安装目录下。可以尝试重新安装 PyQt5 模块来修复这个问题。 3. 检查系统环境变量。在某些情况下,pycharm可能无法正确找到 dll 文件,这是因为缺少相关的系统环境变量。我们可以通过手动设置系统环境变量来解决这个问题。打开"我的电脑",右键选择"属性",然后选择"高级系统设置",在弹出的窗口中选择"环境变量"选项。在"系统变量"中找到 "Path" 变量,然后编辑它,添加 PyQt5 相关 dll 文件的路径。 4. 确保 pycharm 的项目中已安装 PyQt5 模块。在 pycharm 中,可以在项目设置中查看模块的安装情况。如果 PyQt5 模块没有安装,在项目设置中选择 "解释器",然后安装 PyQt5 模块。 5. 检查 pycharm 的配置是否正确。在 pycharm 中,可以检查配置是否正确,选择菜单栏中的 "文件",然后选择 "设置"。在 "设置" 窗口的左侧,选择 "项目",然后选择 "项目解释器"。确保已指定正确的解释器路径。 通过以上几个步骤,我们可以尽可能排除导致 "from pyqt5 import qtwidgets importerror: dll load failed"错误的原因,并尝试解决这个问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值