问题
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.
回答1:
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);
}
回答2:
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 ) ;
}
回答3:
You could disable the layout of the tab.
bool result = false;
if (result == false)
{
tabWidget->widget(1)->layout()->setDisabled(true);
}
回答4:
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);
来源:https://stackoverflow.com/questions/29707959/how-do-i-disable-one-tab-in-a-qtabwidget