2

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.

Iuliu
  • 4,001
  • 19
  • 31
user3734823
  • 99
  • 1
  • 1
  • 10

2 Answers2

1

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.

Jablonski
  • 18,083
  • 2
  • 46
  • 47
0

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 );
MildWolfie
  • 2,492
  • 1
  • 16
  • 27
  • Unfortunately setTabEnabled doesn't hide tab, only repaint it as disable, is there some way to hide tab with setTabEnabled? – Jablonski Dec 17 '14 at 19:31
  • @Chernobyl Not that I know of. I listed my answer as an alternative answer when hiding tabs isn't as important, just making sure they can't be interacted with. – MildWolfie Dec 17 '14 at 19:35
  • @caackley Thanks for the comment! Cuz I needed to hide the tabs, graying out the tabs wasn't an option for me. Thanks tho. – user3734823 Dec 17 '14 at 20:03