Unsolved Plot live data on QtChart
-
@rockon209 In the example they implement a device:
m_device = new XYSeriesIODevice(m_series, this); m_device->open(QIODevice::WriteOnly); m_audioInput->start(m_device);
XYSeriesIODevice is a QIODevice which is writing incoming char* data to the chart. The data is coming from m_audioInput.
In your case you do not need to implement a device: just use a QTimer to read your sensor each second and write the data to the chart in a similar way as in the example. -
@jsulm
ok thanks for the explanation
but it is confusing because in XYSeriesIODevice class have m_series as XYseries and widget class has m_series as Qlineseries
and tried your above suggestion i am getting the following error
error: invalid types 'double[int]' for array subscript
points.append(QPointF(k + size, ((quint8)tempSensor[resolution * k] - 128)/128.0)); -
-
@jsulm
with timer i call the following function every second
void test:: readTemp()
{
double tempSensor= readSignal Sensor(tempChannel);
writeData();
}
void test::writeData()
{
qint64 range = 2000;
QVector<QPointF> oldPoints = m_series->pointsVector();
QVector<QPointF> points;
int resolution = 4;
qint64 maxSize;if (oldPoints.count() < range) { points = m_series->pointsVector(); } else { for (int i = maxSize/resolution; i < oldPoints.count(); i++) points.append(QPointF(i - maxSize/resolution, oldPoints.at(i).y())); } qint64 size = points.count(); for (int k = 0; k < maxSize/resolution; k++) points.append(QPointF(k + size, ((quint8)tempSensor[resolution * k] - 128)/128.0)); m_series->replace(points);
//return maxSize;
} -
@rockon209 said in Plot live data on QtChart:
double tempSensor= readSignal Sensor(tempChannel);
this is not going to work - you store the temperature in a local variable!
Also, this is wrong if tempSensor isn't an array:
points.append(QPointF(k + size, ((quint8)tempSensor[resolution * k] - 128)/128.0));
As I said you need to adjust the code for your needs...
-
@jsulm
then how to make array as i am reading from the sensor every second and getting only one value. -
@rockon209 You don't need any array:
void test:: readTemp() { writeData( readSignal Sensor(tempChannel)); } void test::writeData(const double tempSensor) { qint64 range = 2000; QVector<QPointF> oldPoints = m_series->pointsVector(); QVector<QPointF> points; int resolution = 4; qint64 maxSize; if (oldPoints.count() < range) { points = m_series->pointsVector(); } else { for (int i = maxSize/resolution; i < oldPoints.count(); i++) points.append(QPointF(i - maxSize/resolution, oldPoints.at(i).y())); } qint64 size = points.count(); points.append(QPointF(k + size, ((quint8)tempSensor - 128)/128.0)); m_series->replace(points); }
-
@jsulm
i am getting following error and the application is shutting down
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc -
@rockon209 Please debug you app to see where it crashes...
-
@jsulm
when i read the sensor data it crashes -
@rockon209 Then find out why and fix it...
-
@jsulm
do you know how maxSize value is calculated because i my code maxsize value is very very very big and in think that is the problem -
@rockon209 In the code in the example it is passed as parameter:
qint64 XYSeriesIODevice::writeData(const char * data, qint64 maxSize)
In your code you don't assign anything to it so it has arbitrary value.