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. Bounding Text Being Rendered Using QPainter?
Forum Updated to NodeBB v4.3 + New Features

Bounding Text Being Rendered Using QPainter?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 4.3k 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.
  • L Offline
    L Offline
    LiamMaru
    wrote on last edited by
    #1

    I'm rendering some text using a QPainter, using the following call:

    @painter->drawText(x, y, Bridge::to_qt(geometry.Data.Text.UTF8String));@

    Prior to this, I'm setting the font for the QPainter. How would I find the bounding box of the text plotted at (x y) using a given QFont? Seems like there should be a way...

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      Hi,

      you can use "this":http://doc.qt.nokia.com/4.7/qpainter.html#drawText-13 drawText oberload to get the bounding rectangle.

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dangelog
        wrote on last edited by
        #3

        [quote author="LiamMaru" date="1301932840"]I'm rendering some text using a QPainter, using the following call:

        @painter->drawText(x, y, Bridge::to_qt(geometry.Data.Text.UTF8String));@

        Prior to this, I'm setting the font for the QPainter. How would I find the bounding box of the text plotted at (x y) using a given QFont? Seems like there should be a way...[/quote]

        Since that call doesn't break the text in lines, doesn't handle alignment, etc., you may use QFontMetrics to find out the bounding rectangle of your text.

        Software Engineer
        KDAB (UK) Ltd., a KDAB Group company

        1 Reply Last reply
        0
        • G Offline
          G Offline
          giesbert
          wrote on last edited by
          #4

          But his drawText also does not use line breaking :-)
          But you are right, QFontMetrics is anothe sollution, perhaps the better one.

          Nokia Certified Qt Specialist.
          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dangelog
            wrote on last edited by
            #5

            Actually I was replying to OP, edited the post to make that clear.

            Software Engineer
            KDAB (UK) Ltd., a KDAB Group company

            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