[QT][QML][C++] Draw an interpolated polygon using C++ painter
-
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); }
-
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 -
@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.
-
Something like spline interpolation?!
This example is for
QChart
but maybe you can use spline interpolation for your polygon. -
@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).
-
@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.
but might end being too round. I have not tried it with a shape like yours.
-
@Shadow-01
Hi
but look at the airplane. it does touch the edges.
Outside of such rounded polygon im not sure what to use. -
@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 useQSplineSeries
outside ofQChart
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.
-
@Shadow-01
You could show an image of the final result :) 🙄
So did you use code from link or did you make some yourself ?