Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Qt Quick Controls 2 TabBar/TabButton autosize to button text width
QtWS25 Last Chance

Qt Quick Controls 2 TabBar/TabButton autosize to button text width

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 2 Posters 5.2k 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.
  • L Offline
    L Offline
    larry104
    wrote on 30 Jun 2016, 20:42 last edited by
    #1

    Is there a way that the Qt Quick Controls 2 TabBar/TabButton can auto size to the button text - it seems the current implementation wastes a lot of space even if padding is set to 0.
    Thanks.

    1 Reply Last reply
    1
    • J Offline
      J Offline
      jpnurmi
      wrote on 30 Jun 2016, 21:58 last edited by
      #2

      TabBar distributes the space equally by default, but respects the widths if TabButtons have it explicitly specified. To make buttons to retain their implicit width, you could do:

      TabBar {
          TabButton {
              text: "Foo"
              width: implicitWidth // <==
          }
          ...
      }
      
      1 Reply Last reply
      1
      • L Offline
        L Offline
        larry104
        wrote on 30 Jun 2016, 23:13 last edited by
        #3

        Setting a width works but then I need to manually calculate the width for every button in the TabBar - Is there a way to access the size of the text in the button?

        J 1 Reply Last reply 1 Jul 2016, 06:05
        0
        • L larry104
          30 Jun 2016, 23:13

          Setting a width works but then I need to manually calculate the width for every button in the TabBar - Is there a way to access the size of the text in the button?

          J Offline
          J Offline
          jpnurmi
          wrote on 1 Jul 2016, 06:05 last edited by jpnurmi 7 Jan 2016, 06:09
          #4

          Is it not what you get with implicit width? The implicit width of the button equals to the implicit width of the content item + left padding + right padding. For the built-in styles, the content item is a Text element.

          PS. If you still need to calculate the width of a specific text string, QML TextMetrics (http://doc.qt.io/qt-5/qml-qtquick-textmetrics.html) can do that.

          1 Reply Last reply
          0
          • L Offline
            L Offline
            larry104
            wrote on 1 Jul 2016, 16:44 last edited by
            #5

            if I set width: implicitWidth the text does not show - Looking at the source it should be as you pointed out:

            implicitWidth: Math.max(background ? background.implicitWidth : 0, contentItem.contentWidth + leftPadding + rightPadding)

            But it seems the value is 0

            1 Reply Last reply
            0
            • J Offline
              J Offline
              jpnurmi
              wrote on 2 Jul 2016, 06:56 last edited by jpnurmi 7 Feb 2016, 06:57
              #6

              I can't test right now, but I suspect it could be because it's using contentWidth instead of implicitWidth. Feel free to report a bug. This is something we should ensure that it works and even make it easily configurable.

              Does it resize to contents if you do:

              TabButton {
                  width: contentItem.implicitWidth + leftPadding + rightPadding
              }
              

              ?

              1 Reply Last reply
              1
              • L Offline
                L Offline
                larry104
                wrote on 3 Jul 2016, 16:41 last edited by
                #7

                Yes, that does the trick! Now it's sizing according to the text width.
                Thanks a lot for your help - I will file a bug report
                Thanks again!

                1 Reply Last reply
                0

                5/7

                1 Jul 2016, 16:44

                • Login

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