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
Forum Updated to NodeBB v4.3 + New Features

QPainterPath from list of points

Scheduled Pinned Locked Moved Solved General and Desktop
qpainterpathqgraphicsitemshape
24 Posts 4 Posters 13.5k Views 3 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.
  • 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