2
votes

I have a QTabwidget and 4 QWidget sub-tabs and I'd like to hide 3 sub tabs when I am not using them. With a 'Enable' button, I want the hidden sub-tabs to appear again. In order to hide them, I tried removeTab function as below

ui->tabWidget_2->removeTab(3);
ui->tabWidget_2->removeTab(2);
ui->tabWidget_2->removeTab(1);

But then, I don't know how to reinsert the hidden tabs cuz I do not have the pointer to the hidden tabs. Or is there any other good method to hide them other than removeTab? Please let me know. It'd be really appreciated. Thanks.

2

2 Answers

1
votes

You need store "copy" of your tab somewhere and insert this tab again. For example:

    QMap<int,QPair<QWidget*,QString> > map;
    map.insert(0,QPair<QWidget*,QString>(ui->tabWidget->widget(0),ui->tabWidget->tabText(0)));
    //store index, widget and title of tab
    ui->tabWidget->removeTab(0);
    ui->tabWidget->insertTab(0,map.value(0).first,map.value(0).second);
    //restore data

I can't tell you that it is the best approach, but removeTab removes tab but not your widget. So when I used this code(with QTextEdit as widget inside tab for example) and type some words, my tab was successfully restored and I didn't lose my data. If you use QIcon than you need store this icon too.

0
votes

Alternatively, if you don't mind the tabs being visible so long as they can't be interacted with, you could use the setTabEnabled function.

ui->tabWidget_2->setTabEnabled( 1, enabled );
ui->tabWidget_2->setTabEnabled( 2, enabled );
ui->tabWidget_2->setTabEnabled( 3, enabled );