Qwt - replot不清楚当前的情节

时间:2014-03-17 16:09:39

标签: c++ qt plot qwt

这是我在这里的第一个问题,我相信我还没有看到其他人要求这个具体问题,但我目前正在处理的是一个不想重新绘制的qwt情节。

我想做什么:调用replot()方法清除当前的情节。

我的问题:调用replot()时,不会清除我当前的情节,而且我的情节会在彼此之上绘制。

Here is a link to an image of my problem

从图像中可以看出,新曲线是在现有曲线的基础上绘制的,这就是我想要解决的问题。

以下是我的一些代码:(如果我错过了某些部分,请告诉我)

plot.cpp

#include "plot.h"
#include <qwt_plot.h>
#include <qwt_plot_grid.h>
#include <qwt_plot_layout.h>
#include <qwt_plot_canvas.h>
#include <qwt_plot_curve.h>
#include <qwt_plot_directpainter.h>
#include <qwt_curve_fitter.h>
#include <qwt_painter.h>

class CurveData: public QwtArraySeriesData<QPointF>
{
public:
    CurveData()
    {
    }

    virtual QRectF boundingRect() const
    {
        if ( d_boundingRect.width() < 0.0 )
            d_boundingRect = qwtBoundingRect( *this );

        return d_boundingRect;
    }

    inline void append( const QPointF &point )
    {
        d_samples += point;
    }

    void clear()
    {
        d_samples.clear();
        d_samples.squeeze();
        d_boundingRect = QRectF( 0.0, 0.0, -1.0, -1.0 );
    }
};

Plot::Plot( QWidget *parent ):
    QwtPlot( parent )
{
    d_directPainter = new QwtPlotDirectPainter(this);

    setAutoReplot(false);

    setAxisScale(QwtPlot::yLeft, 0.014,0.016);
    setAxisScale(QwtPlot::xBottom, 0, 1000);

    d_curve = new QwtPlotCurve();
    d_curve->setData(new CurveData());

    d_curve->attach(this);
}

void Plot::AppendPoint(const QPointF &point)
{
    CurveData *data = static_cast<CurveData *>(d_curve->data());
    data->append(point);
}

void Plot::DrawCurveSegment()
{
    CurveData *data = static_cast<CurveData *>(d_curve->data());

    d_directPainter->drawSeries(d_curve, data->size()-11, data->size()-1);
}

void Plot::ClearPlot()
{
    CurveData *data = static_cast<CurveData *>(d_curve->data());
    data->clear();

    QwtPlot::replot();
}

mainwindow.cpp

MainWindow::MainWindow(QWidget *parent):
    QWidget( parent )
{
    d_plot = new Plot();
    counter = 0;
    loopCounter = 0;

// ... //

}

void MainWindow::timerEvent(QTimerEvent *) {
    if (counter>0 && counter%1000==0)
    {
        d_plot->ClearPlot();
        d_plot->replot();
        loopCounter++;
        qDebug()<<"clear!";
    }

    for (int ii=0; ii<10;ii++)
    {
        double y = someArray[counter];
        double x = (double)counter-((double)loopCounter*1000);

        counter++;
        d_plot->AppendPoint(QPointF(x,y));
    }
    d_plot->DrawCurveSegment();
}

如果有人能看到我做错了什么,我将非常感激。

祝你好运!

2 个答案:

答案 0 :(得分:1)

我通过将clearPlot方法更改为以下内容来解决问题:

void Plot::ClearPlot()
{
    CurveData *data = static_cast<CurveData *>(d_curve->data());
    data->clear();

    QwtPlot::replot();
    QwtPlot::repaint();
}

答案 1 :(得分:0)

你也可以这样做:

QwtPlot::detachItems(QwtPlotItem::Rtti_PlotItem, true);
相关问题