Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to expand tabs in QTabWidget

How to expand tabs in QTabWidget

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 5.3k Views
  • 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 Offline
    K Offline
    KelvinSP
    wrote on last edited by KelvinSP
    #1

    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
    0
    • M Offline
      M Offline
      mostefa
      wrote on last edited by
      #2

      Hi @KelvinSP

      What about stylesheet?

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

      For example??

      1 Reply Last reply
      0
      • K Offline
        K Offline
        KelvinSP
        wrote on last edited by KelvinSP
        #3

        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
        0
        • M Offline
          M Offline
          mostefa
          wrote on last edited by
          #4

          @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
          0
          • K Offline
            K Offline
            KelvinSP
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved