Search code examples
qtqt4qtabwidgetqt4.6qtabbar

Qt4 expanding tabs in QTabBar


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);
}

Solution

  • 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.