QCustomPlot Discussion and Comments

Replace Data of a Graph during measurementReturn to overview

Hey guys,

I want to change data of a graph, when new measurement data is ready. The data will be displayed from start of the graph to the end and then data on the start will be replaced by new data. Until now I use QVector to change the data and then I use plot->graph(0)->setData(v1,v2). I only have to replace the value, not the key of the graph. I tried to get access to the QCPDataMap of the graph, but without doing dirty hacks like const_cast... it won't work.
So is there another way of getting access to the stored data in the graph, without even passing the whole data again to it?

one setData overload allows passing in an external data source (QCPDataMap*), if you set the second parameter copy to false.

Note that QCPGraph still takes ownership of the memory, so after removing the QCPGraph, the external QCPDataMap pointer mustn't be used anymore.

I know this is more verbose than having the const removed from the graph->data() method and being able to access the data on-the-fly. The original decision to do it this way was to have people consciously inject an external data source into QCPGraph and realize the implications. With non-const graph->data() I had feared people would too easily shoot themselves in the foot with pointers becoming invalid. However, I don't think it's the perfect way to do it. A cleaner way is on my todo list, I just don't know yet how it will work.

So the only way would be to have an external QCPDataMap, store the data in there and pass the data to the graph with setData(map,false) at this point.

I'm currently trying it with a subclass of QCPGraph, where I return the mData not const. I this works, I'll show up my solution. Hope this is okay as workaround. If this will not work, I will create an external QCPDataMap to store the data to be changed, before passing it to the graph.

I already have tried this, but my app crashed when inserting data to the external map... compiling went fine...

Hey DerManu,

With a little modification of QCPGraph-Class I got it working!! Here is my change for you for inserting data during runtime...

//class QCPGraph
void setData(const double &key, const double &value);

//Implementation
void QCPGraph::setData(const double &key, const double &value)
{
    QCPData newData;
    newData.key = key;
    newData.value = value;
    mData->insert(newData.key, newData);
}

//how to use
double key = 0.1;
double value = 0.9;
plot->graph(0)->setData(key,value);

Hope you will provide this change to QCustomPlot, which is really a good Qt Widget. I use it for plotting Health-Data like Pulsewave on my Nexus 7 (2013) with Qt 5.1.