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. Make the TextField text to fit the TextField height
Forum Updated to NodeBB v4.3 + New Features

Make the TextField text to fit the TextField height

Scheduled Pinned Locked Moved Solved QML and Qt Quick
textfieldqml
9 Posts 6 Posters 3.4k Views 3 Watching
  • 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.
  • M Offline
    M Offline
    Mixlu
    wrote on last edited by
    #1

    Hi,
    I have a problem working on TextField, I have a fixed size for the height and the width of the TextField and I would like that the text displayed in the TextField fit that height.

    Here is the problem that I have (the text in the TextField is the height) :
    alt text

    I have seen fontSizeMode: Text.Fit for Text and I would like something like this but I didn't find the equivalent for TextField, any idea?

    J.HilkJ 1 Reply Last reply
    0
    • M Mixlu

      Hi,
      I have a problem working on TextField, I have a fixed size for the height and the width of the TextField and I would like that the text displayed in the TextField fit that height.

      Here is the problem that I have (the text in the TextField is the height) :
      alt text

      I have seen fontSizeMode: Text.Fit for Text and I would like something like this but I didn't find the equivalent for TextField, any idea?

      J.HilkJ Online
      J.HilkJ Online
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @Mixlu have you tried:

      font.pixelSize: height
      

      ?

      Should work, if you have a fixed hight for your TextField


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      M 1 Reply Last reply
      2
      • sierdzioS Offline
        sierdzioS Offline
        sierdzio
        Moderators
        wrote on last edited by
        #3

        And if @J-Hilk's suggestion fails, you can use TextMetrics component.

        (Z(:^

        1 Reply Last reply
        2
        • J.HilkJ J.Hilk

          @Mixlu have you tried:

          font.pixelSize: height
          

          ?

          Should work, if you have a fixed hight for your TextField

          M Offline
          M Offline
          Mixlu
          wrote on last edited by
          #4

          @J-Hilk, thanks for your reply

          font.pixelSize: height
          

          don't work :
          8e89faea-7aa3-4775-a54b-1c2be3bac2c4-image.png

          @sierdzio, thanks for your reply
          How could I use TextMetrics ? The idea would be to create many TextMetrics and test if it fits the TextField height ?

          1 Reply Last reply
          0
          • sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #5

            Hm, you're right, you want something else than the usual case.

            I guess you could cycle different sizes in a loop and arrive at one which fits.

            don't work :

            You probably need to take borders, margins and padding into account. So try something like:

            font.pixelSize: height * .7
            

            (Z(:^

            M 1 Reply Last reply
            3
            • sierdzioS sierdzio

              Hm, you're right, you want something else than the usual case.

              I guess you could cycle different sizes in a loop and arrive at one which fits.

              don't work :

              You probably need to take borders, margins and padding into account. So try something like:

              font.pixelSize: height * .7
              
              M Offline
              M Offline
              Mixlu
              wrote on last edited by
              #6

              @sierdzio

              Yes you were right, some padding to take in count here.

              What I have done is

              padding: height*0.1
              font.pixelSize: height - topPadding - bottomPadding
              

              and it works pretty well :
              74b57a9e-8c35-4973-a7cd-daabe79614bc-image.png

              Thanks for your help!

              1 Reply Last reply
              4
              • B Offline
                B Offline
                Bob64
                wrote on last edited by
                #7

                I faced a very similar problem to this, so this was a very useful item. However, I found that I needed some further tweaks. For some reason, adjusting by the raw padding was not sufficient to ensure the text fitted in the box, so I fudged it by scaling by 0.9:

                font.pixelSize: 0.9 * (height - topPadding - bottomPadding)

                I also decided on a padding to height ratio of 0.15 but that was just my preference.

                One difference I notice between Windows and Linux is that Linux seems to be adding extra padding at the bottom despite the bottomPadding being reported as equal to the topPadding. I see this extra space with and without centred vertical alignment though maybe I saw a marginal improvement with the alignment set.

                Windows: 8a0a5558-4f89-47f9-9eed-bf77edaaddf4-image.png

                Linux: 610cc7ab-f8ff-42d6-bc46-a2d59f6cb195-image.png

                Note the cursor height on Linux. I'm not sure how much one can read into it, but it seems to me that it does not seem to be aware of that space at the bottom of the text.

                A final observation is that the default padding was 6, independent of height (at least for the range I tried). This is pretty big for a moderate sized text field. As a relative newbie, I guess I wouldn't really have expected to have to mess with the default padding on a text field unless I had some special requirement. A lesson learned!

                1 Reply Last reply
                2
                • S Offline
                  S Offline
                  seyed
                  wrote on last edited by seyed
                  #8

                  Super ugly widget! I always develop my own text field from scratch and I recommend this to you if it is enough for you. Anyway, the built-in widget is very useful IF it works as expected!

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    LynX_Emage
                    wrote on last edited by LynX_Emage
                    #9

                    Because bottomPadding auto create value so make text inside TextField look really weird , so just change this property like below:

                    bottomPadding: height / 5
                    
                    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