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 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
    0
    • V Violet Giraffe

      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 last edited by
      #2

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

      V 1 Reply Last reply
      0
      • ? A Former User

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

        V Offline
        V Offline
        Violet Giraffe
        wrote on 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
        0
        • V Violet Giraffe

          @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 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 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?

            kshegunovK 1 Reply Last reply
            0
            • V Violet Giraffe

              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?

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on 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
              2
              • kshegunovK kshegunov

                @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 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)?

                kshegunovK 1 Reply Last reply
                0
                • V Violet Giraffe

                  @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)?

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on 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

                  • Login

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