Qt World Summit: Submit your Presentation

QTabBar will not expand its tabs

  • I have a QTabBar which is created and added to a vertical layout at runtime.
    It will not resize its tabs even if the setExpanding is set to true.

    m_pTabBar = new QTabBar();
    ((QVBoxLayout*)(centralwidget->layout()))->insertWidget(0, m_pTabBar);

    m_pTabBar->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);

    m_pTabBar->addTab("Login Tab");
    m_pTabBar->addTab("User profile");
    m_pTabBar->addTab("User Session");


    I want to expand the three tabs to cover the entire width of the main window.
    Anyone having any clue why it does not expand?

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Do you mean having each tab taking 1/3rd of the width ? If so you'll have to do it yourself, QTabBar follows the platform style which generally doesn't make tabs look like that.

    On a related note, shouldn't you be using QTabWidget ?

    One last thing:
    @ ((QVBoxLayout*)(centralwidget->layout()))->insertWidget(0, m_pTabBar);@
    This is a bad idea, don't use C style cast when dealing with QObject derived class. Use qobject_cast

  • Hello,

    Yes, having 1/3.333333333 for each tab so the entire width of the tab bar is filled with tabs; that is my goal.

    C style cast is not the problem. I know it is not the way to go but it was quicker to write the code as an example.

    The issue here is the QTabBar which is not resizing the tabs. Why is the "expanding" property there if it does not work? I quote the documentation : "This property holds when expanding is true QTabBar will expand the tabs to use the empty space." It does not do such thing.

    The QTabWidget cannot be used as I have to use QTabBar in a combination with a QStackedWidget.

  • Lifetime Qt Champion

    Well, in fact it is but it's respecting the OS Style. If you set the property to false you'll see that the tabs will stay big enough to contain the text but will not grow if you make it larger.

  • I will have to get in the office on Monday to make some screen shots as I found it very interesting and spent quite some time on it.

  • I got back to my code after some bug fixing and I have created a small application that reproduces the issue I have:
    @<?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
    <widget class="QMainWindow" name="MainWindow">
    <property name="geometry">
    <property name="windowTitle">
    <widget class="QWidget" name="centralwidget">
    <layout class="QVBoxLayout" name="verticalLayout">
    <widget class="QPushButton" name="pushButton">
    <property name="text">

    @#include <QMainWindow>
    #include "ui_tabbar_expand.h"

    class MainWindow : public QMainWindow, public Ui_MainWindow

    MainWindow(QWidget *parent = 0, Qt::WindowFlags flags = 0);
    virtual ~MainWindow(){};

    @#include "mainwindow.h"

    MainWindow::MainWindow(QWidget *parent, Qt::WindowFlags flags)
    :QMainWindow(parent, flags)

    QTabBar* pTabBar = new QTabBar();
    qobject_cast<QVBoxLayout*>(centralwidget->layout())->insertWidget(0, pTabBar);

    //pTabBar->addTab("Login Tab");
    //pTabBar->addTab("User profile");
    //pTabBar->addTab("User Session");

    pTabBar->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);

    pTabBar->addTab("Login Tab");
    pTabBar->addTab("User profile");
    pTabBar->addTab("User Session");


    @#include <qapplication.h>
    #include "mainwindow.h"

    int main(int argc, char *argv[])
    QApplication app(argc, argv);

    MainWindow w;

    return app.exec();

  • Lifetime Qt Champion

    Again, QTabBar is respecting your OS style so it's not a bug or a misbehavior.

    You'll have to subclass QTabBar and reimplement tabSizeHint to achieve what you want.

Log in to reply