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 csv file values and put these values ​​in a graph
Forum Updated to NodeBB v4.3 + New Features

read csv file values and put these values ​​in a graph

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 5 Posters 2.4k Views 1 Watching
  • 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.
  • N Offline
    N Offline
    najlou
    wrote on last edited by VRonin
    #1

    I would like to open a csv file, read these values ​​and draw them in a graph I started the code but I got stuck how to identify the first column for which is the x of our graph and the second column for which is y from our graph you will find attached my code of the opening of the file and the code of the graph
    could you help me

       QFile file(":/exampleTable.csv");
        if ( !file.open(QFile::ReadOnly | QFile::Text) ) {
            qDebug() << "File not exists";
        } else {
            // Create a thread to retrieve data from a file
            QTextStream in(&file);
            //Reads the data up to the end of file
            while (!in.atEnd())
            {
                QString line = in.readLine();
    //Im stuck there
    
    

    graphe code :

    QLineSeries *series = new QLineSeries();
    //![1]
    
    //![2]
        series->append(x, y);
    //![2]
    
    //![3]
        QChart *chart = new QChart();
        chart->legend()->hide();
        chart->addSeries(series);
        chart->createDefaultAxes();
        chart->setTitle("Simple line chart example");
    //![3]
    
    //![4]
        QChartView *chartView = new QChartView(chart);
        chartView->setRenderHint(QPainter::Antialiasing);
    //![4]
    
    
    //![5]
        QMainWindow window;
        window.setCentralWidget(chartView);
        window.resize(400, 300);
        window.show();
    
    Pl45m4P D 2 Replies Last reply
    0
    • N najlou

      I would like to open a csv file, read these values ​​and draw them in a graph I started the code but I got stuck how to identify the first column for which is the x of our graph and the second column for which is y from our graph you will find attached my code of the opening of the file and the code of the graph
      could you help me

         QFile file(":/exampleTable.csv");
          if ( !file.open(QFile::ReadOnly | QFile::Text) ) {
              qDebug() << "File not exists";
          } else {
              // Create a thread to retrieve data from a file
              QTextStream in(&file);
              //Reads the data up to the end of file
              while (!in.atEnd())
              {
                  QString line = in.readLine();
      //Im stuck there
      
      

      graphe code :

      QLineSeries *series = new QLineSeries();
      //![1]
      
      //![2]
          series->append(x, y);
      //![2]
      
      //![3]
          QChart *chart = new QChart();
          chart->legend()->hide();
          chart->addSeries(series);
          chart->createDefaultAxes();
          chart->setTitle("Simple line chart example");
      //![3]
      
      //![4]
          QChartView *chartView = new QChartView(chart);
          chartView->setRenderHint(QPainter::Antialiasing);
      //![4]
      
      
      //![5]
          QMainWindow window;
          window.setCentralWidget(chartView);
          window.resize(400, 300);
          window.show();
      
      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      @najlou said in read csv file values and put these values ​​in a graph:

      while (!in.atEnd())
      {
      QString line = in.readLine();
      //Im stuck there

      This is not that kind of forum where every answer is like "use the search" but there are actually lots of examples how to read / write csv files using Qt :)

      Since you now have your whole line as QString.
      There are also code samples in the documentation. Start here:

      • https://doc.qt.io/qt-5/qstring.html#split

      • https://doc.qt.io/qt-5/qfile.html#reading-files-directly

      • https://stackoverflow.com/questions/27318631/parsing-through-a-csv-file-in-qt


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      4
      • N najlou

        I would like to open a csv file, read these values ​​and draw them in a graph I started the code but I got stuck how to identify the first column for which is the x of our graph and the second column for which is y from our graph you will find attached my code of the opening of the file and the code of the graph
        could you help me

           QFile file(":/exampleTable.csv");
            if ( !file.open(QFile::ReadOnly | QFile::Text) ) {
                qDebug() << "File not exists";
            } else {
                // Create a thread to retrieve data from a file
                QTextStream in(&file);
                //Reads the data up to the end of file
                while (!in.atEnd())
                {
                    QString line = in.readLine();
        //Im stuck there
        
        

        graphe code :

        QLineSeries *series = new QLineSeries();
        //![1]
        
        //![2]
            series->append(x, y);
        //![2]
        
        //![3]
            QChart *chart = new QChart();
            chart->legend()->hide();
            chart->addSeries(series);
            chart->createDefaultAxes();
            chart->setTitle("Simple line chart example");
        //![3]
        
        //![4]
            QChartView *chartView = new QChartView(chart);
            chartView->setRenderHint(QPainter::Antialiasing);
        //![4]
        
        
        //![5]
            QMainWindow window;
            window.setCentralWidget(chartView);
            window.resize(400, 300);
            window.show();
        
        D Offline
        D Offline
        DerReisende
        wrote on last edited by
        #3

        @najlou If I were you I would use this csv parser to properly read the csv file. There is good enough info on how to use on github and then it should be trivial to fill your chart data. I am using the parser so far without any regret.

        JonBJ 1 Reply Last reply
        1
        • D DerReisende

          @najlou If I were you I would use this csv parser to properly read the csv file. There is good enough info on how to use on github and then it should be trivial to fill your chart data. I am using the parser so far without any regret.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @DerReisende
          I have no experience of it, but that looks to my eye like a lot of code! If the OP wants external code at all, I see there is actively-maintained qtcsv, only fair to mention that. Otherwise lots of other simple possibilities on @Pl45m4's stackoveflow page.

          D 1 Reply Last reply
          3
          • JonBJ JonB

            @DerReisende
            I have no experience of it, but that looks to my eye like a lot of code! If the OP wants external code at all, I see there is actively-maintained qtcsv, only fair to mention that. Otherwise lots of other simple possibilities on @Pl45m4's stackoveflow page.

            D Offline
            D Offline
            DerReisende
            wrote on last edited by DerReisende
            #5

            @JonB Well, it is a full-fledge csv parser, not a half-baked quick solution. Integrating it in a cmake project is dead simple - I am a beginner in c++/qt world and it took not more than 10 minutes figuring out how to do it.
            Using the library is simple too:

            #include <csv.hpp>
            
            csv::CSVReader csv_number_reader("file.csv");
            
            for (const csv::CSVRow &row: csv_number_reader) {
                    const auto nodeid = row["NODEID"].get<uint>();
                    const auto nodename = row["NODENAME"].get<std::string>();
                    const auto level = row[2].get<uint>();
                    const auto parent_id = row[3].get<uint>();
                    //now add to dataset
                }
            

            This snippet opens file file.csv and iterates over each row in the file. From there you can easily access the columns either by name (if provided in the csv) or by index. I left the exception handling code but it is also simple to handle errors
            For the OP it should be trivial to extract the contents from the csv and then put it into the chart dataset.

            I agree there are other possibilites but this is the one I know and use.

            JonBJ Pl45m4P 2 Replies Last reply
            0
            • D DerReisende

              @JonB Well, it is a full-fledge csv parser, not a half-baked quick solution. Integrating it in a cmake project is dead simple - I am a beginner in c++/qt world and it took not more than 10 minutes figuring out how to do it.
              Using the library is simple too:

              #include <csv.hpp>
              
              csv::CSVReader csv_number_reader("file.csv");
              
              for (const csv::CSVRow &row: csv_number_reader) {
                      const auto nodeid = row["NODEID"].get<uint>();
                      const auto nodename = row["NODENAME"].get<std::string>();
                      const auto level = row[2].get<uint>();
                      const auto parent_id = row[3].get<uint>();
                      //now add to dataset
                  }
              

              This snippet opens file file.csv and iterates over each row in the file. From there you can easily access the columns either by name (if provided in the csv) or by index. I left the exception handling code but it is also simple to handle errors
              For the OP it should be trivial to extract the contents from the csv and then put it into the chart dataset.

              I agree there are other possibilites but this is the one I know and use.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @DerReisende said in read csv file values and put these values ​​in a graph:

              not a half-baked quick solution

              If you're saying that is qtcsv, I don't see that. I respect your choice to use such a large code base with all sorts of extras for parsing a CSV file. Personally I would not, and I think it's reasonable for the OP/others who come here to be aware there are smaller alternatives. The chances are that people asking here really need the simplest of CSV parsing features. I was merely drawing attention to possible alternatives.

              D 1 Reply Last reply
              0
              • JonBJ JonB

                @DerReisende said in read csv file values and put these values ​​in a graph:

                not a half-baked quick solution

                If you're saying that is qtcsv, I don't see that. I respect your choice to use such a large code base with all sorts of extras for parsing a CSV file. Personally I would not, and I think it's reasonable for the OP/others who come here to be aware there are smaller alternatives. The chances are that people asking here really need the simplest of CSV parsing features. I was merely drawing attention to possible alternatives.

                D Offline
                D Offline
                DerReisende
                wrote on last edited by
                #7

                @JonB Sorry for being unclear on that, I was referring to the original OP´s readLine mechanism as half-baked. I should have made that clearer.

                I am sure qtcsv and other parsers are equally mature and besides the one I currently use I have tried some others as well but they didn´t fit to me. Which doesn't mean that the others are bad. I just use mine and IMHO it is a quick and easy solution for his problem.

                1 Reply Last reply
                0
                • D DerReisende

                  @JonB Well, it is a full-fledge csv parser, not a half-baked quick solution. Integrating it in a cmake project is dead simple - I am a beginner in c++/qt world and it took not more than 10 minutes figuring out how to do it.
                  Using the library is simple too:

                  #include <csv.hpp>
                  
                  csv::CSVReader csv_number_reader("file.csv");
                  
                  for (const csv::CSVRow &row: csv_number_reader) {
                          const auto nodeid = row["NODEID"].get<uint>();
                          const auto nodename = row["NODENAME"].get<std::string>();
                          const auto level = row[2].get<uint>();
                          const auto parent_id = row[3].get<uint>();
                          //now add to dataset
                      }
                  

                  This snippet opens file file.csv and iterates over each row in the file. From there you can easily access the columns either by name (if provided in the csv) or by index. I left the exception handling code but it is also simple to handle errors
                  For the OP it should be trivial to extract the contents from the csv and then put it into the chart dataset.

                  I agree there are other possibilites but this is the one I know and use.

                  Pl45m4P Offline
                  Pl45m4P Offline
                  Pl45m4
                  wrote on last edited by
                  #8

                  @DerReisende

                  Why would one use an extra library, when you can do what you need with 2-3 lines of code using Qt classes which are already there?!


                  If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                  ~E. W. Dijkstra

                  D KroMignonK 2 Replies Last reply
                  0
                  • Pl45m4P Pl45m4

                    @DerReisende

                    Why would one use an extra library, when you can do what you need with 2-3 lines of code using Qt classes which are already there?!

                    D Offline
                    D Offline
                    DerReisende
                    wrote on last edited by
                    #9

                    @Pl45m4 Depending on size and complexity of a given file there may be a lot of good reasons to use an external library and not to reinvent the wheel. Proper error handling, performance and memory management for example.

                    I don't have restrictions to use external libs therefore I use the freedom to choose a library I feel comfortable with. And while his use case may be overkill for this library he may have a problem someday which can't be worked out in 3 SLOC but this code will work as well.

                    Besides this it is just an offer to the OP. I don't force him to use this or any other library but given the fact that he was stuck parsing the data I offer a quick (IMHO well-tested) solution to his problem.

                    1 Reply Last reply
                    0
                    • Pl45m4P Pl45m4

                      @DerReisende

                      Why would one use an extra library, when you can do what you need with 2-3 lines of code using Qt classes which are already there?!

                      KroMignonK Offline
                      KroMignonK Offline
                      KroMignon
                      wrote on last edited by
                      #10

                      @Pl45m4 said in read csv file values and put these values ​​in a graph:

                      Why would one use an extra library, when you can do what you need with 2-3 lines of code using Qt classes which are already there?!

                      It depends on what you want to do.
                      If your file is a "simple" integer value comma/semi-colon separated file, then yes using QString::split() is enough.
                      But if your file contains more complex data, or you want to create a data model from it, then it becomes a more complex task.

                      Re-inventing the wheel is something often done in software world, but must not always be done ;)

                      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                      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