Qt4 expanding tabs in QTabBar
Asked Answered
D

3

6

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);
}
Delete answered 27/2, 2011 at 14:51 Comment(0)
V
3

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.

Violetavioletta answered 1/3, 2011 at 16:4 Comment(1)
Thanks, that did the trick. I wonder, why that piece of info never made it into the online documentation.Delete
E
1

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.

Excommunicate answered 26/7, 2014 at 12:28 Comment(0)
S
0

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.

Schizo answered 29/6, 2013 at 3:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.