Qt4 expanding tabs in QTabBar

6k views Asked by At

I am subclassing QTabWidget to add a QTabBar, who's tabs stretch over the whole width of the tabBar. Therefore I am setting the expanding property to true. This doesn't seem to change anything about the behavior of the tabs.

Did anyone encounter the same problem? I use Qt 4.6 in combination with

TabWidget::TabWidget(QWidget *parent)
{
    tabBar = new QTabBar(this);
    tabBar->setIconSize(QSize(160,160));
    tabBar->setExpanding(true);
    setTabBar(tabBar);
}

EDIT: has been solved, here is how I implemented it, in case anyone is interested:

    tabBar = new QTabBar(this);
    tabBar->setExpanding(true);
    layout = new QVBoxLayout(this);
    setLayout(layout);
    stackedLayout = new QStackedLayout();
    layout->addWidget(tabBar);
    layout->addLayout(stackedLayout);
    connect(tabBar, SIGNAL(currentChanged(int)), stackedLayout, SLOT(setCurrentIndex(int)));

void MainWindow::addTab(QWidget *widget, const QIcon &icon, const QString &label) {
    tabBar->addTab(icon, label);
    stackedLayout->addWidget(widget);
}
3

There are 3 answers

1
baysmith On BEST ANSWER

From the QTabBar source code:

// ... Since we don't set
// a maximum size, tabs will EXPAND to fill up the empty space.
// Since tab widget is rather *ahem* strict about keeping the geometry of the
// tab bar to its absolute minimum, this won't bleed through, but will show up
// if you use tab bar on its own (a.k.a. not a bug, but a feature).

To get around this "feature", you can create your own tab widget using a QTabBar above a widget with a QStackedLayout.

0
Yash On

5.2.0 onwards

QTabWidget::tab-bar {
   min-width: 999999px;
}

It will work. No need to use any combination. You can use QTabWidget. Daniel ans is correct.

0
Jamin Grey On

Building on @baysmith's answer, an easier way to force the QTabWidget to let the QTabBar expand, is to set a stylesheet on your QTabWidget that looks something like this:

QTabWidget::tab-bar {
    width: 999999px;
}

Or another ridiculously large number. If your QTabWidget has the tabs going vertically instead of horizontally, use 'height' instead:

QTabWidget::tab-bar {
    height: 999999px;
}

This seems to work fine for me, with Qt 5.0.1. The tabs expand to fill the space, each getting an equal portion. However, it seems like they intentionally leave enough empty space for one more tab, regardless. But the rest of the space is filled as desired. The empty space might be reserved for the tear/scroll buttons, incase too many tabs get added - but I'm not sure.