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. [QT][QML][C++] Draw an interpolated polygon using C++ painter
Forum Updated to NodeBB v4.3 + New Features

[QT][QML][C++] Draw an interpolated polygon using C++ painter

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 3 Posters 1.4k Views 2 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.
  • S Offline
    S Offline
    Shadow.01
    wrote on 13 Jul 2021, 12:08 last edited by Shadow.01
    #1

    Hi all!

    in my QT / QML application the following C++ class works perfectly and draw a simple polygon formed by six elements in a QML view.

    Now, I want an interpolated polygon such that I don't see a series of lines with edges but a continous line without any edge. How can I transform my class in order to do that?

    Important: I don't use a chart but a normal graph figure; due to this reason, I think that the QTSpline series I already found in some forum are not the solution. But I am obviously opened to any way.

    Important: the line, as in figure, should touch all the edge. Otherwise, the result is not what I want. Due to this reason, the Bezier algorytm is not correct.

    Edit: at the end of the post I explained by putting an image what result I would obtain; thanks again.

    Thank you so much!

    #include "mydiagram.h"
    #include <QPainter>
    #include <string>
    #include <iostream>
    #include <QtCharts>
    #include <QSplineSeries>
    #include <QPoint>
    
    using namespace QtCharts;
    
    
    MyDiagram::MyDiagram(QQuickItem *parent): QQuickPaintedItem(parent)
    {
    
    }
    
    void MyDiagram::paint(QPainter *painterMyDiagram) {
    
        QBrush myBrush(QColor("transparent"));
        QPen myPen(QColor("grey"), 3, Qt::DashDotLine);
        painterMyDiagram->setBrush(myBrush);
        painterMyDiagram->setPen(myPen);
        painterMyDiagram->setRenderHint(QPainter::Antialiasing);
        
        static const QPointF points[6] = {
            QPointF(10.0, 80.0),
            QPointF(20.0, 10.0),
            QPointF(200.0, 30.0),
            QPointF(300.0, 160.0),
            QPointF(250.0, 250.0),
            QPointF(10.0, 80.0)
        };
    
        painterMyDiagram->drawPolyline(points, 6);
    }
    

    Notebook 7 - page 1.png

    P 1 Reply Last reply 13 Jul 2021, 13:53
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 13 Jul 2021, 12:42 last edited by
      #2

      Hi

      • but a continous line without any edge.

      You mean like a curve ish ?
      Currently you are using a polyline and it has sharp edges.

      You mean something like ?
      https://www.toptal.com/c-plus-plus/rounded-corners-bezier-curves-qpainter

      1 Reply Last reply
      2
      • S Offline
        S Offline
        Shadow.01
        wrote on 13 Jul 2021, 13:39 last edited by
        #3

        Thanks for your reply; sorry but Bezier is not my solution because the line should touch all given points. In order to explain me better, I attach an image.

        M 1 Reply Last reply 13 Jul 2021, 13:44
        1
        • S Shadow.01
          13 Jul 2021, 13:39

          Thanks for your reply; sorry but Bezier is not my solution because the line should touch all given points. In order to explain me better, I attach an image.

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 13 Jul 2021, 13:44 last edited by
          #4

          @Shadow-01
          Hi
          super with picture.
          But did you try the RoundedPolygon class from the link?

          It does sort of round the edges of a polygon so might look ok and hits the points.

          S 1 Reply Last reply 13 Jul 2021, 14:03
          0
          • S Shadow.01
            13 Jul 2021, 12:08

            Hi all!

            in my QT / QML application the following C++ class works perfectly and draw a simple polygon formed by six elements in a QML view.

            Now, I want an interpolated polygon such that I don't see a series of lines with edges but a continous line without any edge. How can I transform my class in order to do that?

            Important: I don't use a chart but a normal graph figure; due to this reason, I think that the QTSpline series I already found in some forum are not the solution. But I am obviously opened to any way.

            Important: the line, as in figure, should touch all the edge. Otherwise, the result is not what I want. Due to this reason, the Bezier algorytm is not correct.

            Edit: at the end of the post I explained by putting an image what result I would obtain; thanks again.

            Thank you so much!

            #include "mydiagram.h"
            #include <QPainter>
            #include <string>
            #include <iostream>
            #include <QtCharts>
            #include <QSplineSeries>
            #include <QPoint>
            
            using namespace QtCharts;
            
            
            MyDiagram::MyDiagram(QQuickItem *parent): QQuickPaintedItem(parent)
            {
            
            }
            
            void MyDiagram::paint(QPainter *painterMyDiagram) {
            
                QBrush myBrush(QColor("transparent"));
                QPen myPen(QColor("grey"), 3, Qt::DashDotLine);
                painterMyDiagram->setBrush(myBrush);
                painterMyDiagram->setPen(myPen);
                painterMyDiagram->setRenderHint(QPainter::Antialiasing);
                
                static const QPointF points[6] = {
                    QPointF(10.0, 80.0),
                    QPointF(20.0, 10.0),
                    QPointF(200.0, 30.0),
                    QPointF(300.0, 160.0),
                    QPointF(250.0, 250.0),
                    QPointF(10.0, 80.0)
                };
            
                painterMyDiagram->drawPolyline(points, 6);
            }
            

            Notebook 7 - page 1.png

            P Offline
            P Offline
            Pl45m4
            wrote on 13 Jul 2021, 13:53 last edited by Pl45m4
            #5

            @Shadow-01

            Something like spline interpolation?!

            This example is for QChart but maybe you can use spline interpolation for your polygon.

            • https://doc.qt.io/qt-5/qsplineseries.html

            Wiki:
            https://en.wikipedia.org/wiki/Spline_interpolation


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            S 1 Reply Last reply 13 Jul 2021, 14:05
            0
            • M mrjj
              13 Jul 2021, 13:44

              @Shadow-01
              Hi
              super with picture.
              But did you try the RoundedPolygon class from the link?

              It does sort of round the edges of a polygon so might look ok and hits the points.

              S Offline
              S Offline
              Shadow.01
              wrote on 13 Jul 2021, 14:03 last edited by
              #6

              @mrjj Thanks; rounded polygon doesn't exist under the item that QT creator suggested me in the list of figure I can draw; should I import something special? Or, have you an example?

              Thanks again for your time!

              M 1 Reply Last reply 13 Jul 2021, 14:10
              0
              • P Pl45m4
                13 Jul 2021, 13:53

                @Shadow-01

                Something like spline interpolation?!

                This example is for QChart but maybe you can use spline interpolation for your polygon.

                • https://doc.qt.io/qt-5/qsplineseries.html

                Wiki:
                https://en.wikipedia.org/wiki/Spline_interpolation

                S Offline
                S Offline
                Shadow.01
                wrote on 13 Jul 2021, 14:05 last edited by Shadow.01
                #7

                @Pl45m4 Thanks but not; this is not the way; as told in my question, I already tried but it works only in a chart. And I should draw a free polygon outside a chart. Any other idea?

                (No, SPline in a drawPolyline give an error; only under a chart I can use it).

                P 1 Reply Last reply 13 Jul 2021, 15:51
                0
                • S Shadow.01
                  13 Jul 2021, 14:03

                  @mrjj Thanks; rounded polygon doesn't exist under the item that QT creator suggested me in the list of figure I can draw; should I import something special? Or, have you an example?

                  Thanks again for your time!

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 13 Jul 2021, 14:10 last edited by
                  #8

                  @Shadow-01 said in [QT][QML][C++] Draw an interpolated polygon using C++ painter:

                  thanks; rounded polygon doesn't exist under the item that QT creator suggested me in the list of figure I can draw; should I import something

                  Yes, the link has a that class in the bottom. ITs not a default Qt one.

                  The effect sounds like what you want.

                  alt text

                  but might end being too round. I have not tried it with a shape like yours.

                  S 2 Replies Last reply 13 Jul 2021, 14:14
                  1
                  • M mrjj
                    13 Jul 2021, 14:10

                    @Shadow-01 said in [QT][QML][C++] Draw an interpolated polygon using C++ painter:

                    thanks; rounded polygon doesn't exist under the item that QT creator suggested me in the list of figure I can draw; should I import something

                    Yes, the link has a that class in the bottom. ITs not a default Qt one.

                    The effect sounds like what you want.

                    alt text

                    but might end being too round. I have not tried it with a shape like yours.

                    S Offline
                    S Offline
                    Shadow.01
                    wrote on 13 Jul 2021, 14:14 last edited by Shadow.01
                    #9

                    @mrjj Wait, for sure the line touch the edge? It seems not. It is Bezier algorythm? From the page, it seems so..

                    1 Reply Last reply
                    0
                    • M mrjj
                      13 Jul 2021, 14:10

                      @Shadow-01 said in [QT][QML][C++] Draw an interpolated polygon using C++ painter:

                      thanks; rounded polygon doesn't exist under the item that QT creator suggested me in the list of figure I can draw; should I import something

                      Yes, the link has a that class in the bottom. ITs not a default Qt one.

                      The effect sounds like what you want.

                      alt text

                      but might end being too round. I have not tried it with a shape like yours.

                      S Offline
                      S Offline
                      Shadow.01
                      wrote on 13 Jul 2021, 14:17 last edited by Shadow.01
                      #10

                      @mrjj Thanks but for sure is not the way. My solution should absolutely touch the edge (given points), otherwise the line is not correct. Other solutions? Thanks for your time!

                      M 1 Reply Last reply 13 Jul 2021, 14:27
                      0
                      • S Shadow.01
                        13 Jul 2021, 14:17

                        @mrjj Thanks but for sure is not the way. My solution should absolutely touch the edge (given points), otherwise the line is not correct. Other solutions? Thanks for your time!

                        M Offline
                        M Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 13 Jul 2021, 14:27 last edited by
                        #11

                        @Shadow-01
                        Hi
                        but look at the airplane. it does touch the edges.
                        Outside of such rounded polygon im not sure what to use.

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          Shadow.01
                          wrote on 13 Jul 2021, 14:31 last edited by
                          #12

                          No, unfortunately don't touch. If you see the internal vertical segment of each engine, is for sure longer in the left one. Anyway, thanks. :-(

                          1 Reply Last reply
                          0
                          • S Shadow.01
                            13 Jul 2021, 14:05

                            @Pl45m4 Thanks but not; this is not the way; as told in my question, I already tried but it works only in a chart. And I should draw a free polygon outside a chart. Any other idea?

                            (No, SPline in a drawPolyline give an error; only under a chart I can use it).

                            P Offline
                            P Offline
                            Pl45m4
                            wrote on 13 Jul 2021, 15:51 last edited by Pl45m4
                            #13

                            @Shadow-01 said in [QT][QML][C++] Draw an interpolated polygon using C++ painter:

                            (No, SPline in a drawPolyline give an error; only under a chart I can use it).

                            Read my answer again.
                            I know that you can't use QSplineSeries outside of QChart but you should take a took at it (spline interpolation) and the Wikipedia page, I linked.

                            If there's nothing like this in Qt you have to draw it yourself using mathematics.


                            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                            ~E. W. Dijkstra

                            1 Reply Last reply
                            0
                            • S Offline
                              S Offline
                              Shadow.01
                              wrote on 14 Jul 2021, 11:05 last edited by
                              #14

                              Thanks all so much. I found a solution, by using the Bezier Algorithm with a really small curve factor number. The result is almost the same as desired.

                              M 1 Reply Last reply 14 Jul 2021, 11:33
                              2
                              • S Shadow.01
                                14 Jul 2021, 11:05

                                Thanks all so much. I found a solution, by using the Bezier Algorithm with a really small curve factor number. The result is almost the same as desired.

                                M Offline
                                M Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on 14 Jul 2021, 11:33 last edited by
                                #15

                                @Shadow-01
                                You could show an image of the final result :) 🙄
                                So did you use code from link or did you make some yourself ?

                                1 Reply Last reply
                                0

                                1/15

                                13 Jul 2021, 12:08

                                • Login

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