14

I have a QTabWidget called 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. 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.

Daniel Hedberg
  • 5,677
  • 4
  • 36
  • 61
Joey Kleingers
  • 297
  • 1
  • 4
  • 13

4 Answers4

31

You can enable/disable individual tabs in a QTabWidget using the member function setTabEnabled(int index, bool enable).

Based on your code snippet, it would look like this:

bool result = false;
if (result == false)
{
  tabWidget->setTabEnabled(1, false);
}
Daniel Hedberg
  • 5,677
  • 4
  • 36
  • 61
0

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 ) ;
}
HappyCactus
  • 1,935
  • 16
  • 24
-2

If you use Qt Widgets Application template and Advanced tab's name is tabAdvanced (you can check the name in Object Inspector), this should work:

ui->tabAdvanced->setEnabled(false);
Alexander
  • 44
  • 3
  • what is `tabAdvanced`? – eyllanesc Mar 30 '18 at 08:40
  • should be the name of Advanced tab QWidget, which is the child of `tabWidget`, you can check the name in Object Inspector – Alexander Mar 30 '18 at 08:50
  • 1
    The questioner only wants to disable a QTabBar tab, not the QTabWidget, so your code does not respond to the OP. – eyllanesc Mar 30 '18 at 08:52
  • It's not QTabWidget, it's QWidget. The questioner accepted Daniel Hedberg's answer which seems to disable tabWidget's page, my code does the same but in different way. – Alexander Mar 30 '18 at 09:02
  • If you realize your question does not work since you are assuming many things, it is not general, I recommend you read [answer], if you improve your answer I will remove the downvote. – eyllanesc Mar 30 '18 at 09:08
  • Ok I've edited my answer (not a question) but still I don't understand you claim, I've started my answer from saying that my code is not the general case – Alexander Mar 30 '18 at 09:43
  • Do you think it is practical and elegant to use the name of the widget? A better option would be to use the index of the tab. – eyllanesc Mar 30 '18 at 09:45
  • In this exact case when we don't need to iterate tabs, yes I think using name makes the code more readable. – Alexander Mar 30 '18 at 10:08
-3

You could disable the layout of the tab.

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