I've got a problem with a maintenance of curves' pointers. I've got memory access violation during setting samples (or attaching curve to plot). It happens in 3rd call of updateDisplayingData method. Debugger caught exception in void QwtSeriesStore::setData( QwtSeriesData *series ) in QwtSeriesStore. Below updated SSCCE code:

Header file:

#ifndef PLOT_H
#define PLOT_H

#include <qwt_plot.h>
#include <qwt_legend.h>
#include <qwt_plot_curve.h>

class Plot : public QwtPlot
{
    Q_OBJECT

public:
    explicit Plot(QWidget *parent = 0);
    ~Plot();

    void updateDisplayingData(std::vector<double> data);

private:
    void setUpPlot();
    void setUpCurves();
    void initialXAxisValues();    

    std::vector<double> XAxisValues;
    std::auto_ptr<QwtLegend> legend;
    QwtPlotCurve *aXCurve;
};

#endif // PLOT_H

Source file:

#include "plot.h"

Plot::Plot(QWidget *parent)
: QwtPlot(parent)
{
    setUpPlot();
    setUpCurves();
}

void Plot::setUpPlot()
{
    QwtLegend *legend = new QwtLegend;
    legend->setFrameStyle(QFrame::Box|QFrame::Sunken);
    this->insertLegend(legend, QwtPlot::BottomLegend);
}

void Plot::setUpCurves()
{
    aXCurve = new QwtPlotCurve("Acceleration in X axis");
    aXCurve->setStyle(QwtPlotCurve::Lines);
    aXCurve->setRenderHint(QwtPlotItem::RenderAntialiased);
    aXCurve->setPen(QPen(QColor(150,200,200),2));
}

void Plot::initialXAxisValues()
{
    double time = 0;
    for(int i=0; i<=600; i++)
    {
        XAxisValues.push_back(time);
        time += 0.1;
        qDebug() << XAxisValues[i];
    }
}

void Plot::updateDisplayingData(std::vector<double> data)
{
    this->detachItems(QwtPlotItem::Rtti_PlotItem, true);
    aXCurve->setSamples(QVector<double>::fromStdVector(XAxisValues),QVector<double>::fromStdVector(data));
    aXCurve->attach(this);
    replot();
}

Plot::~Plot()
{
}

It is also notewortly that when I initialize curve each time when I call update (example below) method it works correctly (but it does not meet project requirements).

void Plot::updateDisplayingData(std::vector<double> data)
{
    this->detachItems(QwtPlotItem::Rtti_PlotItem, true);
    QwtPlotCurve *aXCurve1;
    aXCurve1 = new QwtPlotCurve("Acceleration in X axis");
    aXCurve1->setStyle(QwtPlotCurve::Lines);
    aXCurve1->setRenderHint(QwtPlotItem::RenderAntialiased);
    aXCurve1->setPen(QPen(QColor(150,150,200),2));

    aXCurve1->setSamples(QVector<double>::fromStdVector(XAxisValues),QVector<double>::fromStdVector(data));
    aXCurve1->attach(this);
    replot();
}
有帮助吗?

解决方案

I found a bug. It was in

this->detachItems(QwtPlotItem::Rtti_PlotItem, true);

The second parameter define auto deletion item after detach. So value 'true' led to deletion of my curve. When I've changed it to 'false' everythings started work correctly.

其他提示

I had this problem too and it turned out that I had just plain forgotten to initialize the QwtPlotCurve. Apparently I need to step up my compiler’s error checking…

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top