Search code examples
c++qtqwt

QwtPlotCurve with arrow / QwtPainter


I want to draw a QwtCurve with arrow at the end of the curve. After searching a long time, I didn't find a easy way to do that. So I thought about creating a class QwtPlotArrow that inherits from QwtPlotCurve.

I override the drawCurve() function of QwtPlotCurve and tried to use the QwtPainter::drawLine() function to draw a line on my plot. I am doing that like this:

QwtPainter::drawLine(painter, 2, 2, -2, -2);

At the end of the override function I call

QwtPlotCurve::drawCurve(painter, style, xMap, yMap, canvasRect, from, to);

to execude the drawCurve() function of QwtPlotCurve.

Most things are working fine. The drawCurve() function of QwtPlotArrow is called and even the drawCurve() function of QwtPlotCurve is executed afterwards. But the line form (2,2) to (-2,-2) is not drawn on the plot.

That means, that I am dooing something wrong with this command:

QwtPainter::drawLine(painter, 2, 2, -2, -2);

This command does not paint the line to the plot. Even in the source code of QwtPlotCurve I couldn't find how the lines are drawn. For me it looks like the command above is the right one.

What am I doing wrong?


Solution

  • I am not sure, but I think the problem is, that the coordinates have to be maped to the plot. I still don't know how to do that with

    QwtPainter::drawLine(QPainter, 2, 2, -2, -2);
    

    but you can do that with

    QwtPainter::drawPolyline()
    

    Therefore you have to create an QwtPointSeriesData object. You can mapp it to the plot with an QwtPointMapper object.

    Example:

    void QwtPlotArrow::drawCurve(QPainter *painter, int style, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect, int from, int to) const
    {
        QwtPointSeriesData* mySeries = new QwtPointSeriesData();
        mySeries->setSamples(QVector<QPointF>() << QPointF(2, 2) << QPointF(-2, -2);
    
        QwtPainter::drawPolyline(painter, QwtPointMapper().toPolygonF(xMap, yMap, mySeries, from, to));
    
        //Don't forget to execute the drawCurve function from QwtPlotCurve
        QwtPlotCurve::drawCurve(painter, style, xMap, yMap, canvasRect, from, to);
    }