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. Get the coordinate of specific character in a text givent font and string
QtWS25 Last Chance

Get the coordinate of specific character in a text givent font and string

Scheduled Pinned Locked Moved Solved General and Desktop
qfontqfontmetrics
8 Posts 3 Posters 2.8k 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.
  • V Offline
    V Offline
    Violet Giraffe
    wrote on 21 May 2016, 20:19 last edited by Violet Giraffe
    #1

    I have a text (QString), and a QFont that I will use to render this text on the screen. How can I found out either the center coordinate or the bounding rect for a character with index n in the QString?

    ? 1 Reply Last reply 21 May 2016, 20:26
    0
    • V Violet Giraffe
      21 May 2016, 20:19

      I have a text (QString), and a QFont that I will use to render this text on the screen. How can I found out either the center coordinate or the bounding rect for a character with index n in the QString?

      ? Offline
      ? Offline
      A Former User
      wrote on 21 May 2016, 20:26 last edited by
      #2

      @Violet-Giraffe Hi! QFontMetricsF provides the functionality you need.

      V 1 Reply Last reply 21 May 2016, 20:31
      0
      • ? A Former User
        21 May 2016, 20:26

        @Violet-Giraffe Hi! QFontMetricsF provides the functionality you need.

        V Offline
        V Offline
        Violet Giraffe
        wrote on 21 May 2016, 20:31 last edited by
        #3

        @Wieland, thanks for the quick reply, but I don't see which QFontMetricsF method does that (nor do I see any difference from the regular QFontMetrics, other than using floating-point values).

        ? 1 Reply Last reply 21 May 2016, 20:36
        0
        • V Violet Giraffe
          21 May 2016, 20:31

          @Wieland, thanks for the quick reply, but I don't see which QFontMetricsF method does that (nor do I see any difference from the regular QFontMetrics, other than using floating-point values).

          ? Offline
          ? Offline
          A Former User
          wrote on 21 May 2016, 20:36 last edited by
          #4

          @Violet-Giraffe Yeah, it doesn't provide a ready-to-use function for exactly what you want but it has QRectF boundingRect(const QString &text) const and with this you can incrementally compute all said coordinates.

          1 Reply Last reply
          0
          • V Offline
            V Offline
            Violet Giraffe
            wrote on 21 May 2016, 20:40 last edited by
            #5

            I see! So iterate the text one QChar at a time and add up the bounding rects for each one up until the target character?

            K 1 Reply Last reply 21 May 2016, 22:38
            0
            • V Violet Giraffe
              21 May 2016, 20:40

              I see! So iterate the text one QChar at a time and add up the bounding rects for each one up until the target character?

              K Offline
              K Offline
              kshegunov
              Moderators
              wrote on 21 May 2016, 22:38 last edited by kshegunov
              #6

              @Violet-Giraffe
              I think you misunderstood, there's no need for iteration. You can get the character rectangle directly, there's only one additional step to get that rectangle's offset (provided your string doesn't have newlines, tabs and such). For example:

              QFont font; //< The font you're using.
              QFontMetrics metrics(font);
              
              QString string = "mystring";
              int n = 4;
              
              QRect left = metrics.boundingRect(string.mid(0, n));
              QRect character = metrics.boundingRect(string.at(n));
              
              qint32 centerX = left.width() + (character.width() >> 1);
              qint32 centerY = left.height() >> 1;  // Or character.height() divided by two, they should be the same though
              

              Also you can look up this thread.

              Read and abide by the Qt Code of Conduct

              V 1 Reply Last reply 22 May 2016, 08:09
              2
              • K kshegunov
                21 May 2016, 22:38

                @Violet-Giraffe
                I think you misunderstood, there's no need for iteration. You can get the character rectangle directly, there's only one additional step to get that rectangle's offset (provided your string doesn't have newlines, tabs and such). For example:

                QFont font; //< The font you're using.
                QFontMetrics metrics(font);
                
                QString string = "mystring";
                int n = 4;
                
                QRect left = metrics.boundingRect(string.mid(0, n));
                QRect character = metrics.boundingRect(string.at(n));
                
                qint32 centerX = left.width() + (character.width() >> 1);
                qint32 centerY = left.height() >> 1;  // Or character.height() divided by two, they should be the same though
                

                Also you can look up this thread.

                V Offline
                V Offline
                Violet Giraffe
                wrote on 22 May 2016, 08:09 last edited by Violet Giraffe
                #7

                @kshegunov said:

                I think you misunderstood, there's no need for iteration.

                It only occurred to me after I went to sleep last night. That's why coding after midnight is not always a great idea :)

                @kshegunov said:

                qint32 centerY = left.height() >> 1;  // Or character.height() divided by two, they should be the same though
                

                Isn't any modern C++ compiler capable of doing that optimization implicitly (with optimizations enabled)?

                K 1 Reply Last reply 22 May 2016, 11:18
                0
                • V Violet Giraffe
                  22 May 2016, 08:09

                  @kshegunov said:

                  I think you misunderstood, there's no need for iteration.

                  It only occurred to me after I went to sleep last night. That's why coding after midnight is not always a great idea :)

                  @kshegunov said:

                  qint32 centerY = left.height() >> 1;  // Or character.height() divided by two, they should be the same though
                  

                  Isn't any modern C++ compiler capable of doing that optimization implicitly (with optimizations enabled)?

                  K Offline
                  K Offline
                  kshegunov
                  Moderators
                  wrote on 22 May 2016, 11:18 last edited by
                  #8

                  @Violet-Giraffe said:

                  Isn't any modern C++ compiler capable of doing that optimization implicitly (with optimizations enabled)?

                  In all probability, yes; it's just a stupid habit. Compilers weren't so smart before, but they are improving by the minute. You can safely replace it with left.height() / 2.

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  0

                  1/8

                  21 May 2016, 20:19

                  • Login

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