简体   繁体   English

如何禁用QTabWidget中的一个选项卡?

[英]How do I disable one tab in a QTabWidget?

I have a QTabWidget called tabWidget. 我有一个QTabWidget称为tabWidget。 It has three tabs: "Basic", "Advanced", and "Current Structure". 它有三个选项卡:“基本”,“高级”和“当前结构”。 The tabs are displayed in the widget in that order. 选项卡按顺序显示在窗口小部件中。

I want to disable the "Advanced" tab whenever the Boolean result is false. 我想在布尔result为false时禁用“高级”选项卡。 I thought it would be as simple as this code: 我以为它会像这段代码一样简单:

bool result = false;
if (result == false)
{
  tabWidget->widget(1)->setDisabled(true);
}

Unfortunately, this code does not disable the tab, it remains enabled even when I check it: 不幸的是,此代码不会禁用该选项卡,即使我检查它也会保持启用状态:

tabWidget->tabBar()->isTabEnabled(1);  // This returns true

Why doesn't the tab become disabled? 为什么标签不会被禁用? Is there another way to do it? 还有另一种方法吗?

I am using Qt 5.4.0. 我使用的是Qt 5.4.0。

You can enable/disable individual tabs in a QTabWidget using the member function setTabEnabled(int index, bool enable) . 您可以使用成员函数setTabEnabled(int index,bool enable)启用/禁用QTabWidget中的各个选项卡。

Based on your code snippet, it would look like this: 根据您的代码段,它看起来像这样:

bool result = false;
if (result == false)
{
  tabWidget->setTabEnabled(1, false);
}

You can't, not this way. 你不能,不是这样。

You have to iterate through all the children in the Page and disable them. 您必须遍历页面中的所有子项并禁用它们。

Something like this: 像这样的东西:

QList<QWidget*> list = parentWidget->findChildren<QWidget*>() ;
foreach( QWidget* w, list ) {
   w->setEnabled( false ) ;
}

You could disable the layout of the tab. 您可以禁用选项卡的布局。

bool result = false;
if (result == false)
{
  tabWidget->widget(1)->layout()->setDisabled(true);
}

如果您使用Qt Widgets应用程序模板,而高级选项卡的名称是tabAdvanced (您可以在Object Inspector中检查名称),这应该有效:

ui->tabAdvanced->setEnabled(false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM