0

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

Now, I want an interpolated polygon; in other words, I don't want a series of lines with edges but a continuous 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.

Edit: in order to explain better what I need, I attach an image at the end of this post.

#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);
}

enter image description here

halfer
  • 19,824
  • 17
  • 99
  • 186
Archimede
  • 699
  • 3
  • 15
  • 28
  • A little tip: you can make a minimal example (which will help getting better answers quicker) by putting all of your C++ into one file, and the QML in another: https://gist.github.com/mitchcurtis/4792dfaa46b8a40b9ffcbc0e33b20283 – Mitch Jul 13 '21 at 15:16
  • 1
    Bezier is not good for you because you need to have the curves go through the control points. Probably you will need Catmull-Rom Spline here. I needed exactly this in my gear generator. You can find the source code here: https://github.com/bonafid3/Gearszki – Ponzifex Jul 16 '21 at 21:51

2 Answers2

0

Use QPainterPath for drawing such things , your question is like this question

in QML see this Example , you should use Canvas

Parisa.H.R
  • 3,303
  • 3
  • 19
  • 38
0

At the end I use the Bezier algorythm, as described here:

https://www.toptal.com/c-plus-plus/rounded-corners-bezier-curves-qpainter

Not exactly what I was searching for (in this case the line doesn't touch all edges), but by settting a small curve factor is more or less the same. Thanks anyway for helping.

Archimede
  • 699
  • 3
  • 15
  • 28