Qwt定制离散线与连续线

2014年4月21日 由 Creater 留言 »

因为QwtPlotCurve里有如下定义

  enum CurveStyle
    {
        /*!Don't draw a curve. Note: This doesn't affect the symbols.*/
        NoCurve = -1,
        Lines,
        Sticks,
        Steps,
        Dots,
        UserCurve = 100
    };

为了能够定制曲线,需要重载以下函数,在QwtPlotCurve里的实现为

void QwtPlotCurve::drawCurve( QPainter *painter, int style,
    const QwtScaleMap &xMap, const QwtScaleMap &yMap,
    const QRectF &canvasRect, int from, int to ) const
{
    switch ( style )
    {
        case Lines:
            if ( testCurveAttribute( Fitted ) )
            {
                // we always need the complete
                // curve for fitting
                from = 0;
                to = dataSize() - 1;
            }
            drawLines( painter, xMap, yMap, canvasRect, from, to );
            break;
        case Sticks:
            drawSticks( painter, xMap, yMap, canvasRect, from, to );
            break;
        case Steps:
            drawSteps( painter, xMap, yMap, canvasRect, from, to );
            break;
        case Dots:
            drawDots( painter, xMap, yMap, canvasRect, from, to );
            break;
        case NoCurve:
        default:
            break;
    }
}

通过CurveStyle来分发不同的画线方式,我们可以使用UserCurve来区分
1.首先定义一个派生于QwtPlotCurve的类

class MyCurve: public QwtPlotCurve

2.重新定义

virtual void drawCurve( QPainter *p, int style, const QwtScaleMap &xMap,
                            const QwtScaleMap &yMap, const QRectF &canvasRect, int from, int to ) const
    {

        p->setPen(pen);
        switch ( style )
        {
            case QwtPlotCurve::UserCurve:
                drawMyCurve(p,  xMap, yMap, canvasRect, from, to);
                break;
            default:
                 QwtPlotCurve::drawCurve(p, style, xMap, yMap,canvasRect, from, to);
                break;
       }
    }

3.最后重新实现drawMyCurve即可。

至此,完美解决。

广告位

评论已关闭.