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. QPainterPath from list of points
QtWS25 Last Chance

QPainterPath from list of points

Scheduled Pinned Locked Moved Solved General and Desktop
qpainterpathqgraphicsitemshape
24 Posts 4 Posters 13.0k 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.
  • Joel BodenmannJ Offline
    Joel BodenmannJ Offline
    Joel Bodenmann
    wrote on last edited by
    #1

    I have a class named SchematicWire that inherits from QGraphiscItem.The class contains a list of points: QList<QPoint> points. Inside the SchematicWire::paint() function I use QPainter::drawPolyline() to draw the line in the scene. SchematicWire::boundingRect() has been implemented to return a rectangle that contains all points of the line. Everything works fine so far.
    Now, I would like to implement SchematicWire::shape(). I could return a QPainterPath that just is a line consisting of the points stored in the list. However, I would like to add some padding. Therefore, I must create a QPainterPath that's a loop around the actual line with a given padding.
    Can anyone tell me how I would create that QPainterPath the easiest way?

    Here's a picture of a SchematicWireand the boundingRect() around it (there are a few pixels of padding): http://paste.ugfx.org/sores/7505d64a54e0/da1b18bc89fc.jpg

    Industrial process automation software: https://simulton.com
    Embedded Graphics & GUI library: https://ugfx.io

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

      I assume that
      void QPainterPath::addPolygon(const QPolygonF & polygon)
      is no use as u want to have it offset for each line segment?

      Joel BodenmannJ 1 Reply Last reply
      0
      • mrjjM mrjj

        I assume that
        void QPainterPath::addPolygon(const QPolygonF & polygon)
        is no use as u want to have it offset for each line segment?

        Joel BodenmannJ Offline
        Joel BodenmannJ Offline
        Joel Bodenmann
        wrote on last edited by
        #3

        Thanks for the quick reply.
        Well, the problem is that I'd have to build the polygon first. The real question is exactly how to make that polygon.

        For some clarification, this is what I want: http://paste.ugfx.org/sores/7505d64a54e0/264c3e10630e.jpg
        The green line is the QPainterPath that should be returned by SchematicWire::shape().

        Industrial process automation software: https://simulton.com
        Embedded Graphics & GUI library: https://ugfx.io

        mrjjM 1 Reply Last reply
        0
        • Joel BodenmannJ Joel Bodenmann

          Thanks for the quick reply.
          Well, the problem is that I'd have to build the polygon first. The real question is exactly how to make that polygon.

          For some clarification, this is what I want: http://paste.ugfx.org/sores/7505d64a54e0/264c3e10630e.jpg
          The green line is the QPainterPath that should be returned by SchematicWire::shape().

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

          @Joel-Bodenmann
          Ahh you mean the actual outline math
          Well its a well defined problem
          http://www.cescg.org/CESCG99/SKrivograd/

          Not sure there is an easy(er) way.
          Since you must offset each line segments to each side of it. for that u need at least
          some angle.

          mrjjM 1 Reply Last reply
          1
          • mrjjM mrjj

            @Joel-Bodenmann
            Ahh you mean the actual outline math
            Well its a well defined problem
            http://www.cescg.org/CESCG99/SKrivograd/

            Not sure there is an easy(er) way.
            Since you must offset each line segments to each side of it. for that u need at least
            some angle.

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

            oh
            have u tested with void
            QPainter::strokePath(const QPainterPath & path, const QPen & pen)
            I mean , if u build Path with points and then stroke it?

            Joel BodenmannJ 1 Reply Last reply
            0
            • mrjjM mrjj

              oh
              have u tested with void
              QPainter::strokePath(const QPainterPath & path, const QPen & pen)
              I mean , if u build Path with points and then stroke it?

              Joel BodenmannJ Offline
              Joel BodenmannJ Offline
              Joel Bodenmann
              wrote on last edited by Joel Bodenmann
              #6

              Thanks for linking the article - that might be handy in case of I really have to implement this manually.
              QPainter::strokePath won't work because I really need the resulting polygon to be returned by my shape() function.

              The reason I want to do this is because QGraphcisItem::shape() is used to determine when a hover event occurs over an item.
              I want to show up little 'grab handles' (small squares) on the edges of my line in order to allow the user to move around the line/wire segments. Therefore, I need to know whether the mouse is currently hovering above the item.
              Using QGraphicsItem::boundingRect() won't work because you'd get hover events even when you are not above the line (because the rectangle contains a lot of free space).

              Edit: The shape in form of the polygon is also needed in order to know when the line was selected in order to eg. delete it.

              Industrial process automation software: https://simulton.com
              Embedded Graphics & GUI library: https://ugfx.io

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

                Ok. i see.
                So it is not enough to actually test for mouse click on line
                http://stackoverflow.com/questions/26849632/see-if-a-point-lies-on-a-linevector

                I have no good suggestions for easy way. so let's wait and see
                if someone has a nice trick before resorting calculating offsets for each line segments :)

                Joel BodenmannJ 1 Reply Last reply
                0
                • mrjjM mrjj

                  Ok. i see.
                  So it is not enough to actually test for mouse click on line
                  http://stackoverflow.com/questions/26849632/see-if-a-point-lies-on-a-linevector

                  I have no good suggestions for easy way. so let's wait and see
                  if someone has a nice trick before resorting calculating offsets for each line segments :)

                  Joel BodenmannJ Offline
                  Joel BodenmannJ Offline
                  Joel Bodenmann
                  wrote on last edited by
                  #8

                  Thanks for your help, very appreciated!

                  I hope that somebody has a better, easier way to get this done :/

                  Industrial process automation software: https://simulton.com
                  Embedded Graphics & GUI library: https://ugfx.io

                  mrjjM 1 Reply Last reply
                  0
                  • Joel BodenmannJ Joel Bodenmann

                    Thanks for your help, very appreciated!

                    I hope that somebody has a better, easier way to get this done :/

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

                    @Joel-Bodenmann
                    well if no neat tricks comes, here is easy to understand
                    implementation of click a line which would be easy to adapt to
                    trigger when near a line as it uses vectors.
                    https://msdn.microsoft.com/en-us/library/ms969920.aspx

                    Joel BodenmannJ 1 Reply Last reply
                    0
                    • mrjjM mrjj

                      @Joel-Bodenmann
                      well if no neat tricks comes, here is easy to understand
                      implementation of click a line which would be easy to adapt to
                      trigger when near a line as it uses vectors.
                      https://msdn.microsoft.com/en-us/library/ms969920.aspx

                      Joel BodenmannJ Offline
                      Joel BodenmannJ Offline
                      Joel Bodenmann
                      wrote on last edited by
                      #10

                      @mrjj I don't think that that's going to help because the mouse-click-detection should be handled by the QGraphicsView/QGraphicsScene and for that they use QGraphcisItem::shape().

                      Industrial process automation software: https://simulton.com
                      Embedded Graphics & GUI library: https://ugfx.io

                      mrjjM kshegunovK 2 Replies Last reply
                      0
                      • Joel BodenmannJ Joel Bodenmann

                        @mrjj I don't think that that's going to help because the mouse-click-detection should be handled by the QGraphicsView/QGraphicsScene and for that they use QGraphcisItem::shape().

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

                        @Joel-Bodenmann
                        ok, so you must have an actual polygon to return
                        for hit-testing/handles to work.

                        1 Reply Last reply
                        0
                        • Joel BodenmannJ Joel Bodenmann

                          @mrjj I don't think that that's going to help because the mouse-click-detection should be handled by the QGraphicsView/QGraphicsScene and for that they use QGraphcisItem::shape().

                          kshegunovK Offline
                          kshegunovK Offline
                          kshegunov
                          Moderators
                          wrote on last edited by kshegunov
                          #12

                          @Joel-Bodenmann
                          Let me first clarify something, you want to get an envelope around your path, that is larger (respectful to the lines) by some fixed pixel offset? Or you're talking about enveloping a polygon?

                          Right, I read your post again and understand what you mean now. I believe you should implement that yourself, at least I don't know any easy way. I assume your points are ordered, so it's a simple linear algebra problem. You take the points by pairs, and from that pair you can easily calculate the alongside and normal vector for each line. From those two vectors (after normalization) you can build up your envelope. I hope that helps.

                          Kind regards.

                          Read and abide by the Qt Code of Conduct

                          Joel BodenmannJ 1 Reply Last reply
                          0
                          • kshegunovK kshegunov

                            @Joel-Bodenmann
                            Let me first clarify something, you want to get an envelope around your path, that is larger (respectful to the lines) by some fixed pixel offset? Or you're talking about enveloping a polygon?

                            Right, I read your post again and understand what you mean now. I believe you should implement that yourself, at least I don't know any easy way. I assume your points are ordered, so it's a simple linear algebra problem. You take the points by pairs, and from that pair you can easily calculate the alongside and normal vector for each line. From those two vectors (after normalization) you can build up your envelope. I hope that helps.

                            Kind regards.

                            Joel BodenmannJ Offline
                            Joel BodenmannJ Offline
                            Joel Bodenmann
                            wrote on last edited by
                            #13

                            @kshegunov You are correct, I need an envelope around the path that has a few pixels of padding on each side of the line. Like the green line in this crude drawing that I made earlier: http://paste.ugfx.org/sores/7505d64a54e0/264c3e10630e.jpg

                            Industrial process automation software: https://simulton.com
                            Embedded Graphics & GUI library: https://ugfx.io

                            kshegunovK 1 Reply Last reply
                            0
                            • Joel BodenmannJ Joel Bodenmann

                              @kshegunov You are correct, I need an envelope around the path that has a few pixels of padding on each side of the line. Like the green line in this crude drawing that I made earlier: http://paste.ugfx.org/sores/7505d64a54e0/264c3e10630e.jpg

                              kshegunovK Offline
                              kshegunovK Offline
                              kshegunov
                              Moderators
                              wrote on last edited by
                              #14

                              @Joel-Bodenmann
                              Sorry for updating a previous post, I didn't realize you were writing at that same time. Look up my suggestion, and if there's something unclear I'll try to expand.

                              Kind regards.

                              Read and abide by the Qt Code of Conduct

                              Joel BodenmannJ 1 Reply Last reply
                              0
                              • kshegunovK kshegunov

                                @Joel-Bodenmann
                                Sorry for updating a previous post, I didn't realize you were writing at that same time. Look up my suggestion, and if there's something unclear I'll try to expand.

                                Kind regards.

                                Joel BodenmannJ Offline
                                Joel BodenmannJ Offline
                                Joel Bodenmann
                                wrote on last edited by
                                #15

                                @kshegunov
                                Thanks for your answer. I guess I'll have to figure out how to properly implement this in the next couple of days then. I'll definitely publish results here so people don't have to do that in the future themselves :)

                                Industrial process automation software: https://simulton.com
                                Embedded Graphics & GUI library: https://ugfx.io

                                kshegunovK 1 Reply Last reply
                                0
                                • Joel BodenmannJ Joel Bodenmann

                                  @kshegunov
                                  Thanks for your answer. I guess I'll have to figure out how to properly implement this in the next couple of days then. I'll definitely publish results here so people don't have to do that in the future themselves :)

                                  kshegunovK Offline
                                  kshegunovK Offline
                                  kshegunov
                                  Moderators
                                  wrote on last edited by
                                  #16

                                  @Joel-Bodenmann
                                  Also QPainterPathStroker::createStroke coupled with QPainterPath::toFillPolygon might be the easiest way to get what you're after.

                                  Read and abide by the Qt Code of Conduct

                                  Joel BodenmannJ 1 Reply Last reply
                                  0
                                  • kshegunovK kshegunov

                                    @Joel-Bodenmann
                                    Also QPainterPathStroker::createStroke coupled with QPainterPath::toFillPolygon might be the easiest way to get what you're after.

                                    Joel BodenmannJ Offline
                                    Joel BodenmannJ Offline
                                    Joel Bodenmann
                                    wrote on last edited by
                                    #17

                                    @kshegunov
                                    Can you elaborate? I still need to get the outline of the stroked polygon somehow, no?

                                    Industrial process automation software: https://simulton.com
                                    Embedded Graphics & GUI library: https://ugfx.io

                                    kshegunovK 1 Reply Last reply
                                    0
                                    • Joel BodenmannJ Joel Bodenmann

                                      @kshegunov
                                      Can you elaborate? I still need to get the outline of the stroked polygon somehow, no?

                                      kshegunovK Offline
                                      kshegunovK Offline
                                      kshegunov
                                      Moderators
                                      wrote on last edited by kshegunov
                                      #18

                                      @Joel-Bodenmann
                                      Hello,
                                      Surely, note however that I've not tried this. Set the pen width with your desired offset. Create the stroke, with the createStroke method. The outline of the stroke is supposed to be (as per the documentation) the painter path you get from it. Convert the path to a polygon with the toFillPolygon method. If it doesn't work, you can always revert to implementing it yourself, but if it does, well, it looks simpler. :)

                                      Kind regards.

                                      Read and abide by the Qt Code of Conduct

                                      Joel BodenmannJ 1 Reply Last reply
                                      0
                                      • kshegunovK kshegunov

                                        @Joel-Bodenmann
                                        Hello,
                                        Surely, note however that I've not tried this. Set the pen width with your desired offset. Create the stroke, with the createStroke method. The outline of the stroke is supposed to be (as per the documentation) the painter path you get from it. Convert the path to a polygon with the toFillPolygon method. If it doesn't work, you can always revert to implementing it yourself, but if it does, well, it looks simpler. :)

                                        Kind regards.

                                        Joel BodenmannJ Offline
                                        Joel BodenmannJ Offline
                                        Joel Bodenmann
                                        wrote on last edited by
                                        #19

                                        @kshegunov
                                        Thanks for the explanation. The trouble I am having is getting the QPainterPath (that needs to be passed to QPainterPathStroker::createStroke() from my list of lines. The only reasonable option I see is QPainterPath::addPolygon() but that I can't use as I don't have a polygon, I only have a polyline (like a non-closed polygon).

                                        Any thoughts?

                                        Industrial process automation software: https://simulton.com
                                        Embedded Graphics & GUI library: https://ugfx.io

                                        1 Reply Last reply
                                        0
                                        • kshegunovK Offline
                                          kshegunovK Offline
                                          kshegunov
                                          Moderators
                                          wrote on last edited by
                                          #20

                                          @Joel-Bodenmann
                                          Hello,
                                          One thing is to create the path from a polygon (no one says it has to be a closed one ;)). You could also perhaps get it by constructing the path from your points with QPainterPath::moveTo and QPainterPath::lineTo. The latter would be my choice if I were supposed to compose a painter path.

                                          Kind regards.

                                          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