Show/Hide sub-tab on QTabWidget
Asked Answered
V

2

11

Assuming I have a QTabWidget that contains 5 sub-tabs. Now I want to show/hide a sub-tab in one of 5 sub-tabs by following code

ui->twListTabs->widget(0)->hide();           // Hide first sub-tab

But this didn’t work for me. Do you have any solutions?

Thanks!

Verenaverene answered 23/8, 2013 at 4:27 Comment(0)
B
13

You only have the option to use:

void QTabWidget::removeTab(int index)

You need to store the pointer to the QWidget in the tab so that you can later insert it.

You could e.g. do something like:

class TabWidget : public QTabWidget
{
    Q_OBJECT          
    enum tabwidgets {tabwidget1,tabwidget2,...,number_of_tabwidgets};
    QWidget* widgets_[number_of_tabwidgets];
public:
    TabWidget(QWidget* parent = 0) : QWidget(parent)
    {
        for(int i(0); i < number_of_tabwidgets; ++i)
        {
            switch(i)
            {
            case tabwidget1:
                insertTab(i,widgets_[i] = new TabWidget1,QString::number(i));
                ....
            }
        }
    }
};
Burl answered 23/8, 2013 at 5:40 Comment(4)
If you have the tab in the UI Designer of Qt Creator, there's no need for this complexity. Just use removeTab and later, to add it back, just use the findChild function to pull up the tab (it's still there, managed by the UI object, even after being removed from the tab widget). See comment dated Aug 12, 2011 here: qtcentre.org/threads/… (and it works for me, 6 years later).Capua
@Dan Nissenbaum yes it is a matter of coding style. You see a lot of code around storing pointers to various gui controls, but I've also started to use findChild more. Also lambda callback that is available from C++11 reduces the need to store pointers.Burl
@DanNissenbaum : You should put your reply as an answer. Anyway, thank you.Coshow
Qt 5.15 introduced a setTabVisible method, see: doc.qt.io/qt-5/qtabwidget.html#setTabVisibleEllaelladine
A
1

To hide I used:

ui->tabWidget->removeTab(ui->tabWidget->indexOf(ui->tabToBeRemoved));

To show I believe you can insert it back with insertTab() to the same position/index.

In Qt 5.15+ you can use setTabVisible().

Atiana answered 22/9, 2021 at 14:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.