⚠️ Forum Maintenance: Feb 6th, 8am - 14pm (UTC+2)

How to expand tabs in QTabWidget

  • I have a QTabWidget like this:

    alt text

    But I want to expand the tabs to "fill" the entire widget width, like this:

    alt text

    How can I do that?

    I tried to use the setExpanding function:


    But it didn't solve my problem.

    I am using Qt 5.3.2 and Qt Creator 3.2.1


    I found a discussion about this here:


    As the @SGaist answered, I need to subclass QTabBar and reimplement tabSizeHint to achieve that.

    Anyway, I will keep this topic opened for a while because I have hope that someone finds some easiest way to do that.

  • Hi @KelvinSP

    What about stylesheet?

    ui->myTabWidget->setStyleSheet("QTabBar::tab {min-width: 150px;max-width: 400px;}");

    For example??

  • Thanks a lot, @mostefa. It worked and it is very simple.

    I am calculating the width based on the QTabWidget width.

    To get the QTabWidget width correctly I need to get it in the showEvent function:

    void LogListForm::showEvent(QShowEvent *ev)
         * Divide by 2 because we have 2 tabs.
         * I need to decrease 24 pixels to fill the width correctly, I don't know exactly 
         * why 24 pixels, but I found this number by making some tests
        int tabWidth = (ui->myTabWidget->width()/2)-24;
         * Then, I set this tabWidth to the styleSheet.
         * Note: I need to set the previously styleSheet to not lose it
        ui->myTabWidget->setStyleSheet( ui->myTabWidget->styleSheet() +
                                        "QTabBar::tab {"
                                        "width: " + QString::number(tabWidth) + "px; }" );

  • @KelvinSP

    You are welcome !

    if you do not have a minimum and maximum width i think that you can use "width" property, instead of "min-width" and "max-width" ,

    "width: " + QString::number(tabWidth) + "px;"

    But it is just a detail , your code is ok,

    Happy to see that my code helped you ;)

  • @mostefa you're right.
    I will update my code.
    Thanks again.

Log in to reply