c++qtqtabwidgetqtwidgetsqtabbar

How to expand tabs in QTabWidget


I have a QTabWidget like this one:

enter image description here

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

enter image description here

I tried to use the setExpanding function:

ui->myTabWidget->tabBar()->setExpanding(true);

But it didn't work.

How can I do that?

I am using Qt 5.3.2 and Qt Creator 3.2.1.


Solution

  • As mostefa answered here, I can set a fixed width for the tabs using styleSheet.

    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.
         */
        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; }" );
    }