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 to add a text label with every shape I draw using QPainter
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 19 Posters 2.2k 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.
  • B BigBen

    Hi.

    I am drawing several shapes (rectangles, lines, ellipse...) on a label (QLabel) using QPainter. This is working fine.
    However, now I want to add a title/label for each shape I draw after I am done drawing it. So, when I finish drawing a shape, a title (line 1, line 2, rectangle 1, etc. ) for that shape should appear anywhere near the body of that shape.

    I am not able to understand how to achieve this.

    668527f9-b39c-4578-9b66-1f4a9e7e361f-image.png

    S Offline
    S Offline
    sammyadam
    wrote on last edited by sammyadam
    #2

    @BigBen Are you drawing the labels in that same method, or are they drawn somewhere else? Can you show the relevant parts?

    1 Reply Last reply
    0
    • B Offline
      B Offline
      BigBen
      wrote on last edited by
      #3

      Hi.

      I am not drawing the labels yet, as I have no idea how to achieve that.

      The relevant code for drawing the shapes is as follows:
      All this is in one function.

      QPainterPath* ThePath = myShape->myQPainterPath();
      myPen.setColor(myShape->color());
      myPen.setWidth(myShape->thickness());
      myPen.setStyle(myShape->style());
      thisPainter.setPen(myPen);
      thisPainter.drawPath(*ThePath);
      
      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #4

        Hi,

        What is the goal of your application ?

        Depending on that you can either draw the text at the same time as the path or maybe consider using the Graphics View Framework and use different items for the paths and the labels.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • J Offline
          J Offline
          Jhonwalker11
          wrote on last edited by
          #5

          I'm using QPainter to draw various forms (rectangles, lines, ellipses, etc.) on a label (QLabel). Everything is going well.
          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.

          1 Reply Last reply
          0
          • 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
                    wrote on last edited by
                    #10

                    Thank you so much for sharing your answer it was very helpful to me again.

                    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

                      • Login

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