1
votes

I am using QTabWidget class of Qt. In TabWidget i am dynamically adding new tab and setting the TextElideMode to right to display the toolbutton in tabBar.

tabWidget = new QTabWidget(this);
m_addNewTab = new QWidget(tabWidget);
m_addNewGridLayout = new QGridLayout(m_addNewTab);
m_addNewWebView = new MyWebView(m_addNewTab);
widget = new QWidget(m_addNewTab);
tb = new QToolButton(widget);
tb->setFixedHeight(20);
tb->setText("<");
tb1 = new QToolButton(widget);
tb1->setFixedHeight(20);
tb1->setText(">");

m_horizontalLayout = new QHBoxLayout(widget);
m_horizontalLayout->addWidget(tb);
m_horizontalLayout->addWidget(tb1);

Please see the below screen shot for the output of the sample application. When the current tab is selected then both the toolbutton should display and text elide mode should be right but when tab is not selected then toolbutton should not be displayed but text elide mode should be left.

Here in below screen shot i am able to hide and show the toolbutton depending on tab selection but when the tab is not selected text elide mode is setting as right so we are able to see the unnecessary space (check last tab). Setting the text elide mode left also not working because we have already set the toolbutton at left side.

Can someone guide me how to remove the space (last tab from screen shot) when there is not tab selected ?

enter image description here

1
If the QToolButtons hide they should also collapse and use no more any space .. how do you hide them? Could you provide a fullminimal workink example, please.Aaron
For hiding the toolbutton, i am just iterating the parent object and once i get the toolbutton object then it will be hidden by below statement. tb->hide();Neel

1 Answers

0
votes

You'll have to:

  1. Keep track of tab index and corresponding holding widget (let's say std::map<int,QToolButton> toolbutton_by_index
  2. When QTabWidget.currentChanged is emitted, deactivate all widgets except the selected

You can do the 2nd part like this:

std::for_each(toolbutton_by_index.begin(), toolbutton_by_index.end(),
        [&index](auto pair){
            (pair.first == index)?pair.second->hide():pair.second->show()});