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. Read data and add it to a chart
QtWS25 Last Chance

Read data and add it to a chart

Scheduled Pinned Locked Moved Unsolved General and Desktop
chartreal time plotplottingtimerfile read
28 Posts 5 Posters 5.8k 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
    deleted286
    wrote on 4 Jan 2021, 06:51 last edited by
    #1

    Hi everyone. Here is a code script that can write and read data from txt and show it on display in a time interval. I want to convert the screen in a real time chart. I want to add my datas to chart in real time. For example, read data in 0,2 sec and add chart in an order. How can i do such thing?

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QFile>
    #include <QTextStream>
    #include <QMessageBox>
    #include <QTimer>
    #include <QList>
    #include <qlist.h>
    #include <QDebug>
    
     QTimer *timer = new QTimer();
    
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
    
        connect(timer, SIGNAL(timeout()), SLOT(Timer_Slot()));
    
    }
    
    void MainWindow::Timer_Slot() {
    
        QString line = allLines[curIndex];
        ui->plainTextEdit->setPlainText(line);
        if (curIndex < allLines.size() - 1 ) {
              curIndex++;
          }
    
    }
    
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    
    void MainWindow::on_pushButton_2_clicked()  //write
    {
        QFile file("C:/Users/ilknu/Documents/QFileDemo/abc.txt");
        if(!file.open(QFile::WriteOnly | QFile::Text )) {
    
            QMessageBox::warning(this, "title", "file not open");
    
    
        }
    
        QTextStream out (&file);
        QString text = ui->plainTextEdit->toPlainText();
        out << text;
        file.flush();
        file.close();
    
    
    }
    
    void MainWindow::on_pushButton_clicked()  //read
    {
    
        QFile file("C:/Users/ilknu/Documents/QFileDemo/abc.txt");
    
      if(file.open(QIODevice::ReadOnly)) {
          QTextStream in (&file);
          while (!in.atEnd()) {
              QString line = in.readLine();
              allLines.append(line);
          }
    
      file.close();
    
    }
    
      file.close();
      curIndex=0;
      timer->start(200);
    }
    
    J 1 Reply Last reply 4 Jan 2021, 06:54
    0
    • D deleted286
      4 Jan 2021, 06:51

      Hi everyone. Here is a code script that can write and read data from txt and show it on display in a time interval. I want to convert the screen in a real time chart. I want to add my datas to chart in real time. For example, read data in 0,2 sec and add chart in an order. How can i do such thing?

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      #include <QFile>
      #include <QTextStream>
      #include <QMessageBox>
      #include <QTimer>
      #include <QList>
      #include <qlist.h>
      #include <QDebug>
      
       QTimer *timer = new QTimer();
      
      
      MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
          , ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
      
      
          connect(timer, SIGNAL(timeout()), SLOT(Timer_Slot()));
      
      }
      
      void MainWindow::Timer_Slot() {
      
          QString line = allLines[curIndex];
          ui->plainTextEdit->setPlainText(line);
          if (curIndex < allLines.size() - 1 ) {
                curIndex++;
            }
      
      }
      
      
      MainWindow::~MainWindow()
      {
          delete ui;
      }
      
      
      void MainWindow::on_pushButton_2_clicked()  //write
      {
          QFile file("C:/Users/ilknu/Documents/QFileDemo/abc.txt");
          if(!file.open(QFile::WriteOnly | QFile::Text )) {
      
              QMessageBox::warning(this, "title", "file not open");
      
      
          }
      
          QTextStream out (&file);
          QString text = ui->plainTextEdit->toPlainText();
          out << text;
          file.flush();
          file.close();
      
      
      }
      
      void MainWindow::on_pushButton_clicked()  //read
      {
      
          QFile file("C:/Users/ilknu/Documents/QFileDemo/abc.txt");
      
        if(file.open(QIODevice::ReadOnly)) {
            QTextStream in (&file);
            while (!in.atEnd()) {
                QString line = in.readLine();
                allLines.append(line);
            }
      
        file.close();
      
      }
      
        file.close();
        curIndex=0;
        timer->start(200);
      }
      
      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 4 Jan 2021, 06:54 last edited by
      #2

      @suslucoder From where do you want to read the data?

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

      D 1 Reply Last reply 4 Jan 2021, 07:02
      0
      • J jsulm
        4 Jan 2021, 06:54

        @suslucoder From where do you want to read the data?

        D Offline
        D Offline
        deleted286
        wrote on 4 Jan 2021, 07:02 last edited by
        #3

        @jsulm i want to read from txt

        J 1 Reply Last reply 4 Jan 2021, 07:04
        0
        • D deleted286
          4 Jan 2021, 07:02

          @jsulm i want to read from txt

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 4 Jan 2021, 07:04 last edited by
          #4

          @suslucoder Then I'm not sure what exactly you want to do. What do you mean by "real time"? You can simply read the whole file at once and update the chart.

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

          D 1 Reply Last reply 4 Jan 2021, 07:12
          0
          • J jsulm
            4 Jan 2021, 07:04

            @suslucoder Then I'm not sure what exactly you want to do. What do you mean by "real time"? You can simply read the whole file at once and update the chart.

            D Offline
            D Offline
            deleted286
            wrote on 4 Jan 2021, 07:12 last edited by
            #5

            @jsulm I don't want it to read all the data and add it to the chart. It can read the entire file in once, but I want it to add the data to the chart every 0.2 seconds. It should always proceed by adding the most recently read data to the chart

            J 1 Reply Last reply 4 Jan 2021, 08:14
            0
            • D deleted286
              4 Jan 2021, 07:12

              @jsulm I don't want it to read all the data and add it to the chart. It can read the entire file in once, but I want it to add the data to the chart every 0.2 seconds. It should always proceed by adding the most recently read data to the chart

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 4 Jan 2021, 08:14 last edited by
              #6

              @suslucoder said in Read data and add it to a chart:

              but I want it to add the data to the chart every 0.2 seconds

              Then this is not "realtime".
              You already have the file content in allLines and you already have Timer_Slot() slot. So, what prevents you from adding next data line to your chart in that slot?

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

              D 1 Reply Last reply 4 Jan 2021, 08:18
              0
              • J jsulm
                4 Jan 2021, 08:14

                @suslucoder said in Read data and add it to a chart:

                but I want it to add the data to the chart every 0.2 seconds

                Then this is not "realtime".
                You already have the file content in allLines and you already have Timer_Slot() slot. So, what prevents you from adding next data line to your chart in that slot?

                D Offline
                D Offline
                deleted286
                wrote on 4 Jan 2021, 08:18 last edited by
                #7

                @jsulm I dont know how to do it, that is the reason I asked for help

                J 1 Reply Last reply 4 Jan 2021, 08:26
                0
                • D deleted286
                  4 Jan 2021, 08:18

                  @jsulm I dont know how to do it, that is the reason I asked for help

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 4 Jan 2021, 08:26 last edited by
                  #8

                  @suslucoder There are many examples: https://doc.qt.io/qt-5/qtcharts-examples.html
                  Take time to see how data is added to a chart.

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

                  D 1 Reply Last reply 4 Jan 2021, 08:30
                  0
                  • J jsulm
                    4 Jan 2021, 08:26

                    @suslucoder There are many examples: https://doc.qt.io/qt-5/qtcharts-examples.html
                    Take time to see how data is added to a chart.

                    D Offline
                    D Offline
                    deleted286
                    wrote on 4 Jan 2021, 08:30 last edited by
                    #9

                    @jsulm as you said, I already have the file content in allLines. When i do series.append(allLines) to adding chart I get an error. Thank you I will analyze the examples

                    J J 2 Replies Last reply 4 Jan 2021, 08:36
                    0
                    • D deleted286
                      4 Jan 2021, 08:30

                      @jsulm as you said, I already have the file content in allLines. When i do series.append(allLines) to adding chart I get an error. Thank you I will analyze the examples

                      J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 4 Jan 2021, 08:36 last edited by
                      #10

                      @suslucoder I told you in that other thread from you that you can't use a string list there. Please also read documentation...

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

                      1 Reply Last reply
                      1
                      • D deleted286
                        4 Jan 2021, 08:30

                        @jsulm as you said, I already have the file content in allLines. When i do series.append(allLines) to adding chart I get an error. Thank you I will analyze the examples

                        J Online
                        J Online
                        JonB
                        wrote on 4 Jan 2021, 08:36 last edited by
                        #11

                        @suslucoder
                        @jsulm already answered this last question in https://forum.qt.io/topic/122320/how-can-i-add-txt-file-datas-in-qlineseries/2. You cannot add a a string (or string list) to a chart, you have to parse it to generate the actual desired numbers.

                        For adding data every few seconds: whether you read the lines from the file as you go or you read them all into a list in memory, either way on a QTimer::timeout() signal read the next however-many from the file/list and add them to the chart.

                        D 1 Reply Last reply 4 Jan 2021, 08:45
                        1
                        • J JonB
                          4 Jan 2021, 08:36

                          @suslucoder
                          @jsulm already answered this last question in https://forum.qt.io/topic/122320/how-can-i-add-txt-file-datas-in-qlineseries/2. You cannot add a a string (or string list) to a chart, you have to parse it to generate the actual desired numbers.

                          For adding data every few seconds: whether you read the lines from the file as you go or you read them all into a list in memory, either way on a QTimer::timeout() signal read the next however-many from the file/list and add them to the chart.

                          D Offline
                          D Offline
                          deleted286
                          wrote on 4 Jan 2021, 08:45 last edited by
                          #12

                          @JonB Okey. I understand. I should use another struct instead of string list, right?

                          J J 2 Replies Last reply 4 Jan 2021, 08:49
                          0
                          • D deleted286
                            4 Jan 2021, 08:45

                            @JonB Okey. I understand. I should use another struct instead of string list, right?

                            J Online
                            J Online
                            JonB
                            wrote on 4 Jan 2021, 08:49 last edited by
                            #13

                            @suslucoder
                            Well it's not another "struct". You presumably have a file containing the text of numbers, perhaps separated by commas or new lines or something. You need to convert those to X,Y numeric values, and then pass those (or QPointFs) to add to the chart series. It doesn't matter whether you do that at the time of reading in the file or just before adding them to the series, but you will need to do so at one of those two stages.

                            1 Reply Last reply
                            0
                            • D deleted286
                              4 Jan 2021, 08:45

                              @JonB Okey. I understand. I should use another struct instead of string list, right?

                              J Offline
                              J Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on 4 Jan 2021, 08:52 last edited by
                              #14

                              @suslucoder said in Read data and add it to a chart:

                              I should use another struct instead of string list, right?

                              You should use what the method to set the data expects. That's why I suggested to read the documentation: it is documented quite clearly.

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

                              D 1 Reply Last reply 4 Jan 2021, 09:17
                              1
                              • J jsulm
                                4 Jan 2021, 08:52

                                @suslucoder said in Read data and add it to a chart:

                                I should use another struct instead of string list, right?

                                You should use what the method to set the data expects. That's why I suggested to read the documentation: it is documented quite clearly.

                                D Offline
                                D Offline
                                deleted286
                                wrote on 4 Jan 2021, 09:17 last edited by
                                #15

                                @jsulm ıs there any example that doing exactly what you said?

                                J 1 Reply Last reply 4 Jan 2021, 09:19
                                0
                                • D deleted286
                                  4 Jan 2021, 09:17

                                  @jsulm ıs there any example that doing exactly what you said?

                                  J Offline
                                  J Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on 4 Jan 2021, 09:19 last edited by jsulm 1 Apr 2021, 09:21
                                  #16

                                  @suslucoder I already posted a link to charts examples.
                                  For example: https://doc.qt.io/qt-5/qtcharts-temperaturerecords-example.html
                                  And you still did not tell us what data you actually want to show and what type of chart you need...

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

                                  1 Reply Last reply
                                  1
                                  • mrjjM Offline
                                    mrjjM Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on 4 Jan 2021, 09:52 last edited by
                                    #17

                                    Hi
                                    Please show one line from the "C:/Users/ilknu/Documents/QFileDemo/abc.txt"
                                    so we know how data is. else it's impossible to give any hints on how you can show that in a chart.

                                    D 1 Reply Last reply 4 Jan 2021, 10:39
                                    0
                                    • mrjjM mrjj
                                      4 Jan 2021, 09:52

                                      Hi
                                      Please show one line from the "C:/Users/ilknu/Documents/QFileDemo/abc.txt"
                                      so we know how data is. else it's impossible to give any hints on how you can show that in a chart.

                                      D Offline
                                      D Offline
                                      deleted286
                                      wrote on 4 Jan 2021, 10:39 last edited by
                                      #18

                                      @mrjj Here it is

                                      1991 01 213.5 136.9 0.64 220.5 147.6 229.4 205.5 8 17.4
                                      1991 02 270.2 167.5 0.62 221.5 147.6 243.0 206.3 10 18.4

                                      J 1 Reply Last reply 4 Jan 2021, 10:49
                                      0
                                      • D deleted286
                                        4 Jan 2021, 10:39

                                        @mrjj Here it is

                                        1991 01 213.5 136.9 0.64 220.5 147.6 229.4 205.5 8 17.4
                                        1991 02 270.2 167.5 0.62 221.5 147.6 243.0 206.3 10 18.4

                                        J Online
                                        J Online
                                        JonB
                                        wrote on 4 Jan 2021, 10:49 last edited by JonB 1 Apr 2021, 10:53
                                        #19

                                        @suslucoder

                                        QString line = read_one_line_from_file();
                                        QStringList list = line.split(QLatin1Char(' '), Qt::SkipEmptyParts);
                                        for (const QString &entry : list)
                                        {
                                            double num = entry.toDouble();    // or `unsigned num = entry.toUInt()` for the first 2 numbers on each line, by the look of it
                                            // do whatever is necessary with each `double` number retrieved
                                            // such as adding pairs directly to the chart, or building a `QLineSeries`, or whatever
                                        }
                                        
                                        D 1 Reply Last reply 4 Jan 2021, 11:03
                                        1
                                        • J JonB
                                          4 Jan 2021, 10:49

                                          @suslucoder

                                          QString line = read_one_line_from_file();
                                          QStringList list = line.split(QLatin1Char(' '), Qt::SkipEmptyParts);
                                          for (const QString &entry : list)
                                          {
                                              double num = entry.toDouble();    // or `unsigned num = entry.toUInt()` for the first 2 numbers on each line, by the look of it
                                              // do whatever is necessary with each `double` number retrieved
                                              // such as adding pairs directly to the chart, or building a `QLineSeries`, or whatever
                                          }
                                          
                                          D Offline
                                          D Offline
                                          deleted286
                                          wrote on 4 Jan 2021, 11:03 last edited by
                                          #20

                                          @JonB Thank you. Where should I place the code script?

                                          J 1 Reply Last reply 4 Jan 2021, 12:10
                                          0

                                          5/28

                                          4 Jan 2021, 07:12

                                          23 unread
                                          • Login

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