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 remove extra spaces from character using QFontMetrics?
Forum Updated to NodeBB v4.3 + New Features

How remove extra spaces from character using QFontMetrics?

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 516 Views 1 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.
  • Z Offline
    Z Offline
    zabitqt
    wrote on 18 Feb 2023, 09:48 last edited by
    #1

    Hi, I'm studying QPainter my goal is to get the width size of a character with QFontMetrics. The problem is that width is returned with extra spaces around character. I don't want this.

    Editor_Screenshot_20230218_104102.png

    As you can see Hello! has between characters some space, it's not near vertical line green o yellow.

    1 Reply Last reply
    0
    • Z Offline
      Z Offline
      zabitqt
      wrote on 19 Feb 2023, 09:44 last edited by
      #2

      is there someone that can help me removing extra spaces from character?

      J 1 Reply Last reply 19 Feb 2023, 11:15
      0
      • Z zabitqt
        19 Feb 2023, 09:44

        is there someone that can help me removing extra spaces from character?

        J Offline
        J Offline
        JonB
        wrote on 19 Feb 2023, 11:15 last edited by JonB
        #3

        @zabitqt
        I do not know what these extra spaces you show are/come from. Certainly I would not expect printing Hello in a font to produce as much spacing as that. There is a picture at int QFontMetrics::horizontalAdvance(QChar ch) const, but I don't think your spacing is to do with that? Suggest you provide a minimal compilable example.

        Z 1 Reply Last reply 20 Feb 2023, 17:09
        1
        • S Offline
          S Offline
          SimonSchroeder
          wrote on 20 Feb 2023, 08:47 last edited by
          #4

          Smart people with knowledge of typography have decided this behavior long before computers were a thing. If you don't know typography it is a bad idea to change this.

          Have a look at kerning and letter spacing inside the QFont class.

          1 Reply Last reply
          0
          • J JonB
            19 Feb 2023, 11:15

            @zabitqt
            I do not know what these extra spaces you show are/come from. Certainly I would not expect printing Hello in a font to produce as much spacing as that. There is a picture at int QFontMetrics::horizontalAdvance(QChar ch) const, but I don't think your spacing is to do with that? Suggest you provide a minimal compilable example.

            Z Offline
            Z Offline
            zabitqt
            wrote on 20 Feb 2023, 17:09 last edited by
            #5

            @JonB If I draw Hello I see it correctly but if I draw character per character I see a space between character.

            J S 2 Replies Last reply 20 Feb 2023, 18:38
            0
            • Z zabitqt
              20 Feb 2023, 17:09

              @JonB If I draw Hello I see it correctly but if I draw character per character I see a space between character.

              J Offline
              J Offline
              JonB
              wrote on 20 Feb 2023, 18:38 last edited by
              #6

              @zabitqt
              I can only guess you are advancing too much then. Maybe show the code which does this?

              1 Reply Last reply
              0
              • Z zabitqt
                20 Feb 2023, 17:09

                @JonB If I draw Hello I see it correctly but if I draw character per character I see a space between character.

                S Offline
                S Offline
                SimonSchroeder
                wrote on 21 Feb 2023, 07:32 last edited by
                #7

                @zabitqt said in How remove extra spaces from character using QFontMetrics?:

                If I draw Hello I see it correctly but if I draw character per character I see a space between character.

                This is because the font has embedded information how to adjust the distance between specific neighboring characters. If you draw them separately, this information is lost for the font renderer.

                Here is the definition of kerning from Wikipedia:

                In typography, kerning is the process of adjusting the spacing between characters in a proportional font, usually to achieve a visually pleasing result. Kerning adjusts the space between individual letterforms.

                I am not sure if Qt provides any way to get to this kind of information. You can disable kerning (for a whole word and not individual letters) and see if you get the same result when you are drawing the letters separately. If this is the case you know that you need to further research kerning.

                1 Reply Last reply
                1

                1/7

                18 Feb 2023, 09:48

                • Login

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