Error when using object of QChart type
Moved
Solved
General and Desktop
-
- I'm having trouble when using object of QChart type. I want to append points to the chart and make it scrollable but there appears to be some errors in cpp file.
#include "myserialport.h" #include <QSerialPortInfo> #include <QDebug> //#include <QTimer> #include <QTime> #include <QDateTime> #include <math.h> #include <QtCharts/QValueAxis> #include <QLine> #include <QtCharts/QChart> QT_USE_NAMESPACE int count=0; MySerialPort::MySerialPort(QObject *parent) : QObject(parent),m_series(0) { myPort=new QSerialPort; serialBuffer = ""; parsed_data = ""; temperature_value = 0.0; //m_series=0; } MySerialPort::~MySerialPort() { delete myPort; } double MySerialPort::readData_slot() { QByteArray buff; QStringList buffer_split = serialBuffer.split(","); // split the serialBuffer string, parsing with ',' as the separator if(buffer_split.length() < 3){ buff=myPort->readAll(); serialBuffer = serialBuffer + QString::fromStdString(buff.toStdString()); buff.clear(); } else{ // the second element of buffer_split is parsed correctly, update the temperature value on temp_lcdNumber serialBuffer = ""; //qDebug() << buffer_split<< "\n"; parsed_data = buffer_split[1]; //temperature_value = (9/5.0) * (parsed_data.toDouble()) + 32; // convert to fahrenheit temperature_value = parsed_data.toDouble(); // celsius qDebug() << "Temperature: " << temperature_value << "\n"; emit receiveData(temperature_value); } return temperature_value; } void MySerialPort::plot_chart(double x, double y) { m_series->append(x,y); //******ERROR***** // need to add scroll here QChart chart; //******ERROR***** chart.scroll(x,y); //******ERROR***** }
- This is my qml file
import QtQuick 2.0 import QtQuick.Layouts 1.12 import QtQuick.Controls 2.5 import QtCharts 2.15 Item { id:root signal sendSettingInfoSignal(int state) signal sendDataSignal(string data) property double rmsec:0 Rectangle{ //create border Layout.fillWidth: true Layout.fillHeight: true Layout.columnSpan:2 border.width: 1 border.color: "gray" height: 4 //display chart in the screen ChartView { id: chart anchors.fill:parent antialiasing: true animationOptions: ChartView.AllAnimations LineSeries { id: line name: "Realtime data chart" axisY: ValuesAxis{ min: 0.0 max: 40.0 tickCount: 6 } // axisX: DateTimeAxis{ // tickCount: 5 // format: "hh:mm:ss" //// max: new Date(2022,5,6,9,49,00) //// min: new Date(2022,5,6,9,48,00) // } axisX: ValuesAxis{ min: 0.0 max: 20.0 } } } } //update time with data Timer{ id: refreshTimer interval: 500 running: true repeat: true onTriggered: { rmsec++; if(cpp_obj.readIsMyPortOpen()){ //line.append(rmsec,cpp_obj.readData_slot()); cpp_obj.plot_chart(rmsec,cpp_obj.readData_slot()); } } } }
- These are my errors:
-
@Duc-Nguyen Did you add
QT += charts
to your pro file as shown in https://doc.qt.io/qt-5/qtcharts-index.html ?
-
Thanks for your reply. That's what I forgot to do :(
-
@Duc-Nguyen Please use the debugger to get the stack trace after crash
-
- Is m_series a valid pointer?
- Why do you create chart as local variable? You know that it will be deleted as soon as plot_chart() finishes?