Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Error when using object of QChart type
Qt 6.11 is out! See what's new in the release blog

Error when using object of QChart type

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 1.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Duc Nguyen
    wrote on last edited by
    #1
    • 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:
      cd4f9384-66a6-4877-8490-311ef9e4c153-image.png
    jsulmJ 1 Reply Last reply
    0
    • D Duc Nguyen
      • 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:
        cd4f9384-66a6-4877-8490-311ef9e4c153-image.png
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Duc-Nguyen Did you add

      QT += charts
      

      to your pro file as shown in https://doc.qt.io/qt-5/qtcharts-index.html ?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      D 1 Reply Last reply
      0
      • D Offline
        D Offline
        Duc Nguyen
        wrote on last edited by
        #3

        Thanks for your reply. That's what I forgot to do :(

        1 Reply Last reply
        0
        • jsulmJ jsulm

          @Duc-Nguyen Did you add

          QT += charts
          

          to your pro file as shown in https://doc.qt.io/qt-5/qtcharts-index.html ?

          D Offline
          D Offline
          Duc Nguyen
          wrote on last edited by
          #4

          @jsulm after I added that to my pro file and run the code, an error message appeared:
          1a77330d-daf4-406b-845f-08eff04b9fe8-image.png

          jsulmJ 1 Reply Last reply
          0
          • D Duc Nguyen

            @jsulm after I added that to my pro file and run the code, an error message appeared:
            1a77330d-daf4-406b-845f-08eff04b9fe8-image.png

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Duc-Nguyen Please use the debugger to get the stack trace after crash

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            D 1 Reply Last reply
            0
            • jsulmJ jsulm

              @Duc-Nguyen Please use the debugger to get the stack trace after crash

              D Offline
              D Offline
              Duc Nguyen
              wrote on last edited by
              #6

              @jsulm My screen showed this when I started debugger:
              5adaf38e-1e4c-42ed-b707-f8438638490b-image.png

              jsulmJ 1 Reply Last reply
              0
              • D Duc Nguyen

                @jsulm My screen showed this when I started debugger:
                5adaf38e-1e4c-42ed-b707-f8438638490b-image.png

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @Duc-Nguyen

                1. Is m_series a valid pointer?
                2. Why do you create chart as local variable? You know that it will be deleted as soon as plot_chart() finishes?

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                2

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved