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

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.
  • M mrjj
    27 Dec 2015, 17:39

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

    J Offline
    J Offline
    Joel Bodenmann
    wrote on 27 Dec 2015, 17:46 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

    M 1 Reply Last reply 27 Dec 2015, 18:10
    0
    • J Joel Bodenmann
      27 Dec 2015, 17:46

      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().

      M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 27 Dec 2015, 18:10 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.

      M 1 Reply Last reply 27 Dec 2015, 18:14
      1
      • M mrjj
        27 Dec 2015, 18:10

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

        M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 27 Dec 2015, 18:14 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?

        J 1 Reply Last reply 27 Dec 2015, 18:32
        0
        • M mrjj
          27 Dec 2015, 18:14

          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?

          J Offline
          J Offline
          Joel Bodenmann
          wrote on 27 Dec 2015, 18:32 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
          • M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 27 Dec 2015, 18:41 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 :)

            J 1 Reply Last reply 27 Dec 2015, 18:46
            0
            • M mrjj
              27 Dec 2015, 18:41

              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 :)

              J Offline
              J Offline
              Joel Bodenmann
              wrote on 27 Dec 2015, 18:46 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

              M 1 Reply Last reply 27 Dec 2015, 18:54
              0
              • J Joel Bodenmann
                27 Dec 2015, 18:46

                Thanks for your help, very appreciated!

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

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 27 Dec 2015, 18:54 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

                J 1 Reply Last reply 27 Dec 2015, 19:02
                0
                • M mrjj
                  27 Dec 2015, 18:54

                  @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

                  J Offline
                  J Offline
                  Joel Bodenmann
                  wrote on 27 Dec 2015, 19:02 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

                  M K 2 Replies Last reply 27 Dec 2015, 19:04
                  0
                  • J Joel Bodenmann
                    27 Dec 2015, 19:02

                    @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().

                    M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 27 Dec 2015, 19:04 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
                    • J Joel Bodenmann
                      27 Dec 2015, 19:02

                      @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().

                      K Offline
                      K Offline
                      kshegunov
                      Moderators
                      wrote on 27 Dec 2015, 19:10 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

                      J 1 Reply Last reply 27 Dec 2015, 19:18
                      0
                      • K kshegunov
                        27 Dec 2015, 19:10

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

                        J Offline
                        J Offline
                        Joel Bodenmann
                        wrote on 27 Dec 2015, 19:18 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

                        K 1 Reply Last reply 27 Dec 2015, 19:32
                        0
                        • J Joel Bodenmann
                          27 Dec 2015, 19:18

                          @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

                          K Offline
                          K Offline
                          kshegunov
                          Moderators
                          wrote on 27 Dec 2015, 19:32 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

                          J 1 Reply Last reply 27 Dec 2015, 19:45
                          0
                          • K kshegunov
                            27 Dec 2015, 19:32

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

                            J Offline
                            J Offline
                            Joel Bodenmann
                            wrote on 27 Dec 2015, 19:45 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

                            K 1 Reply Last reply 27 Dec 2015, 21:53
                            0
                            • J Joel Bodenmann
                              27 Dec 2015, 19:45

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

                              K Offline
                              K Offline
                              kshegunov
                              Moderators
                              wrote on 27 Dec 2015, 21:53 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

                              J 1 Reply Last reply 28 Dec 2015, 00:09
                              0
                              • K kshegunov
                                27 Dec 2015, 21:53

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

                                J Offline
                                J Offline
                                Joel Bodenmann
                                wrote on 28 Dec 2015, 00:09 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

                                K 1 Reply Last reply 28 Dec 2015, 00:18
                                0
                                • J Joel Bodenmann
                                  28 Dec 2015, 00:09

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

                                  K Offline
                                  K Offline
                                  kshegunov
                                  Moderators
                                  wrote on 28 Dec 2015, 00:18 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

                                  J 1 Reply Last reply 28 Dec 2015, 13:12
                                  0
                                  • K kshegunov
                                    28 Dec 2015, 00: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.

                                    J Offline
                                    J Offline
                                    Joel Bodenmann
                                    wrote on 28 Dec 2015, 13:12 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
                                    • K Offline
                                      K Offline
                                      kshegunov
                                      Moderators
                                      wrote on 28 Dec 2015, 15:23 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
                                      • Chris KawaC Offline
                                        Chris KawaC Offline
                                        Chris Kawa
                                        Lifetime Qt Champion
                                        wrote on 28 Dec 2015, 15:49 last edited by Chris Kawa
                                        #21

                                        As mentioned above you can use QPainterPathStroker. The problem is it will create tiny loops on the inside of the joints, so you need to simplify the path afterwards.
                                        Sample code:

                                        QVector<QPoint> points { QPoint(30,30), QPoint(100, 100), QPoint(200,50) };
                                        
                                        QPainterPath basePath;
                                        basePath.addPolygon(QPolygon(points));
                                        
                                        QPainterPathStroker str;
                                        str.setCapStyle(Qt::RoundCap);
                                        str.setWidth(10.0);
                                        
                                        QPainterPath resultPath = str.createStroke(basePath).simplified();
                                        
                                        J 1 Reply Last reply 28 Dec 2015, 16:27
                                        2
                                        • Chris KawaC Chris Kawa
                                          28 Dec 2015, 15:49

                                          As mentioned above you can use QPainterPathStroker. The problem is it will create tiny loops on the inside of the joints, so you need to simplify the path afterwards.
                                          Sample code:

                                          QVector<QPoint> points { QPoint(30,30), QPoint(100, 100), QPoint(200,50) };
                                          
                                          QPainterPath basePath;
                                          basePath.addPolygon(QPolygon(points));
                                          
                                          QPainterPathStroker str;
                                          str.setCapStyle(Qt::RoundCap);
                                          str.setWidth(10.0);
                                          
                                          QPainterPath resultPath = str.createStroke(basePath).simplified();
                                          
                                          J Offline
                                          J Offline
                                          Joel Bodenmann
                                          wrote on 28 Dec 2015, 16:27 last edited by
                                          #22

                                          Wow, I am deeply impressed. Using the code shown by @Chris-Kawa gave exactly the desired result:
                                          Alt text

                                          Thank you very much for your help, guys. Very appreciated!

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

                                          1 Reply Last reply
                                          0

                                          12/24

                                          27 Dec 2015, 19:10

                                          • Login

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