Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved How to expand tabs in QTabWidget

    General and Desktop
    2
    5
    3383
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • K
      KelvinSP last edited by KelvinSP

      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:

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

      But it didn't solve my problem.

      I am using Qt 5.3.2 and Qt Creator 3.2.1

      Update:

      I found a discussion about this here:

      https://forum.qt.io/topic/47404/qtabbar-will-not-expand-its-tabs/7

      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.

      1 Reply Last reply Reply Quote 0
      • M
        mostefa last edited by

        Hi @KelvinSP

        What about stylesheet?

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

        For example??

        1 Reply Last reply Reply Quote 0
        • K
          KelvinSP last edited by KelvinSP

          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; }" );
          }
          
          1 Reply Last reply Reply Quote 0
          • M
            mostefa last edited by

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

            1 Reply Last reply Reply Quote 0
            • K
              KelvinSP last edited by

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

              1 Reply Last reply Reply Quote 0
              • First post
                Last post