Qt 设置选项卡栏

Qt Setting A TabBar

本文关键字:选项 设置 Qt      更新时间:2023-10-16

我正在使用Qt框架制作应用程序,遇到了一个问题:我像这样定义一个 TabWidget

QTabWidget *armaTab = new QTabWidget();
armaTab->setContentsMargins(0, 0, 0, 0);
armaTab->setTabPosition(QTabWidget::North);
armaTab->setObjectName(QString::fromUtf8("armaTab"));

然后我尝试像这样添加一个 QTabBar:

QTabBar *tabBar = new QTabBar();
tabBar->setContentsMargins(0, 0, 0, 0);
tabBar->setFont(*font);
tabBar->setObjectName(QString::fromUtf8("armaTabBar"));
armaTab->setTabBar(tabBar);

我收到此错误:

error: 'void QTabWidget::setTabBar(QTabBar*)' is protected
error: within this context

有人可以解释一下我的错误在哪里吗?

这只是意味着您必须注意才能访问组件: 从文档:

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

将对话框的 QTabBar 标题替换为选项卡栏 tb。请注意,必须在添加任何选项卡之前调用它,否则行为未定义。

我认为您可以简单地对 QTabWidget 进行子类以访问受保护的成员......