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.
  • 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
                            • Chris KawaC Offline
                              Chris KawaC Offline
                              Chris Kawa
                              Lifetime Qt Champion
                              wrote on 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();
                              
                              Joel BodenmannJ 1 Reply Last reply
                              2
                              • Chris KawaC Chris Kawa

                                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();
                                
                                Joel BodenmannJ Offline
                                Joel BodenmannJ Offline
                                Joel Bodenmann
                                wrote on 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
                                • mrjjM Offline
                                  mrjjM Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #23

                                  well what can we say ?
                                  Chris rules :)

                                  Joel BodenmannJ 1 Reply Last reply
                                  1
                                  • mrjjM mrjj

                                    well what can we say ?
                                    Chris rules :)

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

                                    @mrjj Well, thanks to you and @kshegunov too. You guys are amazing!

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

                                    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