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.
  • 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
              • C Online
                C Online
                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
                • C 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
                  • M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 28 Dec 2015, 16:28 last edited by
                    #23

                    well what can we say ?
                    Chris rules :)

                    J 1 Reply Last reply 28 Dec 2015, 16:30
                    1
                    • M mrjj
                      28 Dec 2015, 16:28

                      well what can we say ?
                      Chris rules :)

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

                      24/24

                      28 Dec 2015, 16:30

                      • Login

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