ingo
ingo

Reputation: 856

QTabWidget Hide and Show tabs

I have some problems with QTabWidget. In case of the missing Hide functionality I have to build my own. According to the documentation I use removeTab and insertTab, but with insert Tab I have a problem to show the Tab page that is removed.

I use to add

  RibbonTabContent *ribbonTabContent = new RibbonTabContent;
  QTabWidget::addTab(ribbonTabContent, tabIcon, tabName);

To remove is use:

void Ribbon::hideTab(const QString &tabName)
{
  // Find ribbon tab
  for (int i = 0; i < count(); i++)
  {
    if (tabText(i).toLower() == tabName.toLower())
    {
       QTabWidget::removeTab(i);
      break;
    }
  }
}

Both functions are working, pWidget is always null. But now the insert function do not work well. I think there I have a problem, but do not understand my problem.

void Ribbon::showTab(const QString &tabName){

    // Find ribbon tab
    QWidget* pWidget= QTabWidget::findChild<RibbonTabContent *>(tabName);
    if(pWidget){
        QTabWidget::insertTab(2,pWidget, tabName);
    }
}

Maybe someone can help me out?

Upvotes: 2

Views: 5769

Answers (3)

alextoind
alextoind

Reputation: 1203

Since Qt 5.15 it is also possible to use setTabVisible:

void QTabWidget::setTabVisible(int index, bool visible)

If visible is true, the page at position index is visible; otherwise the page at position index is hidden. The page's tab is redrawn appropriately.If visible is true, the page at position index is visible; otherwise the page at position index is hidden. The page's tab is redrawn appropriately.

Upvotes: 4

Jamin Grey
Jamin Grey

Reputation: 10495

It is unfortunate that QTabBar is unable to 'hide' a tab.

Here is my very easy work-around: mark the tabs 'disabled' instead (e.g. ui->tabWidget->setTabEnabled(tabIndex, false);).

Then, use stylesheets to style the "disabled" tab as entirely invisible and taking up no space:

QTabBar::tab:disabled
{
    min-width: 0px;
    max-width: 0px; 
    color: rgba(0,0,0,0);
    background-color: rgba(0,0,0,0);
}

This works near-perfectly for me, with the only downside being that you can't have both disabled and "hidden" tabs in the same tabbar. However, usually I want one or the other, not both in the same bar.

Upvotes: 0

Silvano Cerza
Silvano Cerza

Reputation: 970

If you call QTabWidget::removeTab you remove the tab at the specified index from the children tree of your QTabWidget, the tab instance is not actually deleted though, so when you search for that same tab with QTabWidget::findChild you can't find it because it's not a child of your QTabWidget anymore. From the code you show I think you probably would not find it anyway since findChild searches for a widget with the specified objectName but you never set it for your tab.

A solution would be to store the removed tabs and then restore them when you please.

Assuming m_hiddenTabs is a QHash<QString, QWidget*> or QMap<QString, QWidget*> you could try something like this.

void Ribbon::hideTab(const QString &tabName)
{
  // Find ribbon tab
  for (int i = 0; i < count(); i++)
  {
    if (tabText(i).toLower() == tabName.toLower())
    {
       m_hiddenTabs.insert(tabName.toLower(), QTabWidget::widget(i));
       QTabWidget::removeTab(i);
       break;
    }
  }
}

void Ribbon::showTab(const QString &tabName){

    // Find ribbon tab
    auto tab = m_hiddenTabs.take(tabName.toLower());
    if(tab){
        QTabWidget::insertTab(2, tab, tabName);
    }
}

Upvotes: 3

Related Questions