How do I disable one tab in a QTabWidget?
Asked Answered
T

4

16

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.

Thorne answered 17/4, 2015 at 19:35 Comment(0)
M
33

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);
}
Mustard answered 17/4, 2015 at 20:22 Comment(1)
Or more succinctly: tabWidget->setTabEnabled(1, result);Butter
T
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 ) ;
}
Terefah answered 17/4, 2015 at 19:49 Comment(1)
This is what I needed. It prevents change of tab contents, but retains visibility of the tab.Injustice
W
-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);
Workroom answered 30/3, 2018 at 8:35 Comment(8)
what is tabAdvanced?Hisakohisbe
should be the name of Advanced tab QWidget, which is the child of tabWidget, you can check the name in Object InspectorWorkroom
The questioner only wants to disable a QTabBar tab, not the QTabWidget, so your code does not respond to the OP.Hisakohisbe
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.Workroom
If you realize your question does not work since you are assuming many things, it is not general, I recommend you read How to Answer, if you improve your answer I will remove the downvote.Hisakohisbe
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 caseWorkroom
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.Hisakohisbe
In this exact case when we don't need to iterate tabs, yes I think using name makes the code more readable.Workroom
K
-3

You could disable the layout of the tab.

bool result = false;
if (result == false)
{
  tabWidget->widget(1)->layout()->setDisabled(true);
}
Kristakristal answered 17/4, 2015 at 20:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.