Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to add a text label with every shape I draw using QPainter
Qt 6.12 LTS is out! See what's new in the release blog

How to add a text label with every shape I draw using QPainter

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 23 Posters 4.8k Views 2 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.
  • A Offline
    A Offline
    adamarehart
    wrote on last edited by
    #6

    Now I want to give each form I design a title or label after I finish sketching it. As a result, whenever I'm done sketching a form, its title (such as line 1, line 2, rectangle 1, etc.) ought to be visible somewhere close to the shape's body.

    mrjjM 1 Reply Last reply
    0
    • A adamarehart

      Now I want to give each form I design a title or label after I finish sketching it. As a result, whenever I'm done sketching a form, its title (such as line 1, line 2, rectangle 1, etc.) ought to be visible somewhere close to the shape's body.

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #7

      @adamarehart
      Is there a question hidden there ?

      1 Reply Last reply
      1
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #8

        @Birdwell
        Do you have a question on how to draw some text near the lines of your shapes or is that ok now?

        1 Reply Last reply
        0
        • JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #9

          @Birdwell
          Next to where you call thisPainter.drawPath(*ThePath); to draw the shape call thisPainter.drawText(...) You will need to set the point or rectangle where you want the text based on the point/rectangle where you are drawing the shape. You will want to adjust from that one to be somewhere a bit outside it so that your text does not write onto the shape.

          1 Reply Last reply
          1
          • W Offline
            W Offline
            wudier
            Banned
            wrote on last edited by
            #10
            This post is deleted!
            1 Reply Last reply
            0
            • J Offline
              J Offline
              Jhonsonedward123
              wrote on last edited by
              #11
              This post is deleted!
              1 Reply Last reply
              0
              • J Offline
                J Offline
                johns Madwell
                wrote on last edited by
                #12

                To label your shapes, store each object's geometry and title in a custom list of structs. Inside your paintEvent(), iterate through this list to render your shapes.

                Immediately after drawing a shape, use painter.drawText() to render its title. Calculate the text position using the shape’s bounding coordinates (like rect.topLeft()) with a small vertical offset, ensuring labels appear consistently near the body of each shape.

                1 Reply Last reply
                0
                • W Offline
                  W Offline
                  wademathew09
                  wrote on last edited by
                  #13

                  Is there any question related to draw some text near the lines of your shape?

                  1 Reply Last reply
                  0
                  • I Offline
                    I Offline
                    iskills
                    Banned
                    wrote on last edited by
                    #14
                    This post is deleted!
                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      gb_whatguru
                      Banned
                      wrote on last edited by
                      #15
                      This post is deleted!
                      1 Reply Last reply
                      0
                      • JKSHJ JKSH locked this topic on

                      • Login

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