qtqt4qtabwidget

QTabWidget how to hide pane only?


I have added a QToolButton as corner widget in QTabWidget which is checkable. I want to hide all tabs (panes only) when the tool button is unchecked. I tried to connect button's signal clicked(bool) with all tab's setVisible(bool) slot not working but. I also connected tabwidget's setvisible to the signal but complete widget became invisible(it was a silly trial). Is there any way to make only pane invisible and tab bar will not disappear ?


Edit: Code (ui have a tabwidget and two tabs namely tab and tab_2)

ui->setupUi(this);
QToolButton * b = new QToolButton;
b->setCheckable(true);
b->setChecked(true);
b->setAutoRaise(true);
b->setText("Hide Tabs");
ui->tabWidget->setCornerWidget(b);
connect(b,SIGNAL(clicked()),ui->tab,SLOT(hide()));
connect(b,SIGNAL(clicked()),ui->tab_2,SLOT(hide()));

Solution

  • Here is my take on this. I've created a class that inherits QTabWidget. What I do is; set the "maximum vertical size of QTabWidget" to its tabBars height to hide the panels.

    It is a hacky solution and I had to add some extra lines to deal with quirks.

    file: hidabletabwidget.h


    #ifndef HIDABLETABWIDGET_H
    #define HIDABLETABWIDGET_H
    
    #include <QTabWidget>
    #include <QAction>
    
    class HidableTabWidget : public QTabWidget
    {
        Q_OBJECT
    public:
        explicit HidableTabWidget(QWidget *parent = 0);
        QAction hideAction;
    
    private slots:
        void onHideAction(bool checked);
        void onTabBarClicked();
    };
    
    #endif // HIDABLETABWIDGET_H
    

    file: hidablewidget.cpp


    #include "hidabletabwidget.h"
    #include <QTabBar>
    #include <QToolButton>
    
    HidableTabWidget::HidableTabWidget(QWidget *parent) :
        QTabWidget(parent),
        hideAction("▾", this)
    {
        hideAction.setCheckable(true);
        hideAction.setToolTip("Hide Panels");
        QToolButton* hideButton = new QToolButton();
        hideButton->setDefaultAction(&hideAction);
        hideButton->setAutoRaise(true);
        this->setCornerWidget(hideButton);
    
        connect(&hideAction, SIGNAL(toggled(bool)), this, SLOT(onHideAction(bool)));
        connect(this, SIGNAL(tabBarClicked(int)), this, SLOT(onTabBarClicked()));
    }
    
    void HidableTabWidget::onHideAction(bool checked)
    {
        if (checked)
        {
            this->setMaximumHeight(this->tabBar()->height());
            this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
        }
        else
        {
            this->setMaximumHeight(QWIDGETSIZE_MAX); // by default widgets can expand to a maximum sized defined by this macro
            this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
        }
    }
    
    void HidableTabWidget::onTabBarClicked()
    {
        hideAction.setChecked(false);
    }
    

    To use this, you can simply "promote" your QTabWidget to "HidableTabWidget" using qt designer.

    And here is how it looks on my system:

    hidable widget shown and hidden states screenshot