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. Create a matrix from a .csv file
Forum Updated to NodeBB v4.3 + New Features

Create a matrix from a .csv file

Scheduled Pinned Locked Moved Solved General and Desktop
140 Posts 2 Posters 58.8k Views 2 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.
  • A AliM93
    30 May 2020, 22:16

    No the tableview was just a check for me, but i don't have to use it. i just want to show the data in a grid and that's all.
    i declare it as a QString, is the same of e StringList? yes the order it's like that anyway

    So i have to add a file.h and file.cpp or i can work on this?

    M Offline
    M Offline
    mrjj
    Lifetime Qt Champion
    wrote on 30 May 2020, 22:25 last edited by mrjj
    #17

    @AliM93 said in Create a matrix from a .csv file:

    No the tableview was just a check for me, but i don't have to use it. i just want to show the data in a grid and that's all.
    i declare it as a QString, is the same of e StringList? yes the order it's like that anyway

    Ok, just asking as using a model allows to share data views which is super nice if one needs to be able to edit it and have it reflected at once in some other view. But if you just use it to show data and dont want to edit it. then i would just use
    some small structure to all data needed for the grid display.

    So i have to add a file.h and file.cpp or i can work on this?

    You mean if you can use Matrix class for this custom widget ?
    Yes, but its a QDialog so not perfect fit. ( unless you always want the Grid to popup over the main window)

    A 1 Reply Last reply 30 May 2020, 22:30
    1
    • M mrjj
      30 May 2020, 22:25

      @AliM93 said in Create a matrix from a .csv file:

      No the tableview was just a check for me, but i don't have to use it. i just want to show the data in a grid and that's all.
      i declare it as a QString, is the same of e StringList? yes the order it's like that anyway

      Ok, just asking as using a model allows to share data views which is super nice if one needs to be able to edit it and have it reflected at once in some other view. But if you just use it to show data and dont want to edit it. then i would just use
      some small structure to all data needed for the grid display.

      So i have to add a file.h and file.cpp or i can work on this?

      You mean if you can use Matrix class for this custom widget ?
      Yes, but its a QDialog so not perfect fit. ( unless you always want the Grid to popup over the main window)

      A Offline
      A Offline
      AliM93
      wrote on 30 May 2020, 22:30 last edited by AliM93
      #18

      @mrjj from the main_window a open all the other window. and the matrix is a part of another QDialog. now i splith things in order to understand what i have to do but then i have to embed this as a part o QDaialog window. maybe afert a pushbutton clicked i open my grid, the user select and then push another button and i return to my previous GUI with point selected saved.

      So what i have to do now is to add a customgrid.h customgrid.cpp, but what i have to select? to add? if it is not a dialog? a cpp headers and cpp source file separately? or a cpp class?

      M 1 Reply Last reply 30 May 2020, 22:35
      0
      • A AliM93
        30 May 2020, 22:30

        @mrjj from the main_window a open all the other window. and the matrix is a part of another QDialog. now i splith things in order to understand what i have to do but then i have to embed this as a part o QDaialog window. maybe afert a pushbutton clicked i open my grid, the user select and then push another button and i return to my previous GUI with point selected saved.

        So what i have to do now is to add a customgrid.h customgrid.cpp, but what i have to select? to add? if it is not a dialog? a cpp headers and cpp source file separately? or a cpp class?

        M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 30 May 2020, 22:35 last edited by
        #19

        @AliM93
        Hi
        Well if you want to pop it up anyway, you could leave it as QDialog and draw directly to it but
        could be more handy if it was standalone and you would add it to a QDialog but this is not critical.

        well you just inherit QWidget instead of QDialog and change in cpp where it calls base in constructor.
        xxx) : QDialog(parent) {} -> ) : QWidget(parent) {}

        class GridWidget : public QWidget
        
        1 Reply Last reply
        1
        • A Offline
          A Offline
          AliM93
          wrote on 30 May 2020, 22:43 last edited by AliM93
          #20
          This post is deleted!
          M 1 Reply Last reply 30 May 2020, 22:50
          0
          • A AliM93
            30 May 2020, 22:43

            This post is deleted!

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 30 May 2020, 22:50 last edited by
            #21

            @AliM93
            Ah sorry my bad.
            You have an UI file with it and inside that it says its still a QDialog so that what it complains about
            So you must open that UI in text editor and change type ( Creator should be close)
            or remove that it has one.
            That is matrix.h line 23
            and also the setupUI thing and the ui thing line 20

            Its hard to show when you use images for code.

            So do you need UI file ?
            To add other widgets on top of grid ?

            A 1 Reply Last reply 30 May 2020, 22:54
            0
            • M mrjj
              30 May 2020, 22:50

              @AliM93
              Ah sorry my bad.
              You have an UI file with it and inside that it says its still a QDialog so that what it complains about
              So you must open that UI in text editor and change type ( Creator should be close)
              or remove that it has one.
              That is matrix.h line 23
              and also the setupUI thing and the ui thing line 20

              Its hard to show when you use images for code.

              So do you need UI file ?
              To add other widgets on top of grid ?

              A Offline
              A Offline
              AliM93
              wrote on 30 May 2020, 22:54 last edited by
              #22

              @mrjj what if i do from new? maybe more clear for me to learn something. and sorry fro the images, up to now i will paste the code.

              M 1 Reply Last reply 30 May 2020, 22:57
              0
              • A AliM93
                30 May 2020, 22:54

                @mrjj what if i do from new? maybe more clear for me to learn something. and sorry fro the images, up to now i will paste the code.

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 30 May 2020, 22:57 last edited by
                #23

                @AliM93
                Yes lets do a clean widget. I think its good to learn as Creator can really help in this.
                give me 5. ill make some shots.

                1 Reply Last reply
                1
                • M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 30 May 2020, 23:08 last edited by mrjj
                  #24

                  1:
                  alt text
                  Choose New file or Project

                  2:
                  alt text
                  Select C++ class

                  3:
                  Give it a name and set base class to Qwidget
                  alt text

                  press NEXT, and then next again.

                  4:
                  we now have the new class and its cpp and .h added to the project
                  alt text

                  Lets add PaintEvent and MousePress

                  Right click the name of the class in the .h file (MatrixWidget)
                  and select the Refactor menu and add virtual function
                  alt text

                  Now type Paint in top
                  and check the paintEvent function
                  make sure the Insert option is as shown

                  alt text

                  Dont press OK yet!

                  now type Press in top (where you had paint)
                  and find the MousePress function and check that.

                  Now Press ok.

                  Then we are back to the code with our new functions.
                  alt text

                  Press F4 to switch to cpp file and see it has given you the bodies also.

                  alt text

                  Now you made your custom widget. ( it might seems a lot first time, but its very fast when used to it :)

                  So next part is to add code to paintEvent to draw something.
                  We also need your old reader code to get the data and stuff into our structure.

                  A 1 Reply Last reply 30 May 2020, 23:21
                  0
                  • M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 30 May 2020, 23:14 last edited by
                    #25

                    hi
                    I hope the above did work for you.
                    If yes. Next step is the structure to hold the data.

                    
                    struct DataPoint {
                        int x;
                        int y;
                        bool value;
                        QColor DrawColor; // we change this from selected / not selected.
                        bool isSelected; // we set this when we click on it.
                    };
                    
                    const int max_x = 14;
                    const int max_y = 21;
                    
                    class MatrixWidget : public QWidget
                    {
                        Q_OBJECT
                        DataPoint Data[max_x][max_y];  /// this is where we want to load the data to
                    public:
                        explicit MatrixWidget(QWidget *parent = nullptr);
                    
                    signals:
                    
                    
                        // QWidget interface
                    protected:
                        virtual void mousePressEvent(QMouseEvent *event) override;
                        virtual void paintEvent(QPaintEvent *event) override;
                    };
                    
                    1 Reply Last reply
                    1
                    • M mrjj
                      30 May 2020, 23:08

                      1:
                      alt text
                      Choose New file or Project

                      2:
                      alt text
                      Select C++ class

                      3:
                      Give it a name and set base class to Qwidget
                      alt text

                      press NEXT, and then next again.

                      4:
                      we now have the new class and its cpp and .h added to the project
                      alt text

                      Lets add PaintEvent and MousePress

                      Right click the name of the class in the .h file (MatrixWidget)
                      and select the Refactor menu and add virtual function
                      alt text

                      Now type Paint in top
                      and check the paintEvent function
                      make sure the Insert option is as shown

                      alt text

                      Dont press OK yet!

                      now type Press in top (where you had paint)
                      and find the MousePress function and check that.

                      Now Press ok.

                      Then we are back to the code with our new functions.
                      alt text

                      Press F4 to switch to cpp file and see it has given you the bodies also.

                      alt text

                      Now you made your custom widget. ( it might seems a lot first time, but its very fast when used to it :)

                      So next part is to add code to paintEvent to draw something.
                      We also need your old reader code to get the data and stuff into our structure.

                      A Offline
                      A Offline
                      AliM93
                      wrote on 30 May 2020, 23:21 last edited by AliM93
                      #26

                      sorry ok i put the function

                      M 1 Reply Last reply 30 May 2020, 23:25
                      0
                      • A AliM93
                        30 May 2020, 23:21

                        sorry ok i put the function

                        M Offline
                        M Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 30 May 2020, 23:25 last edited by
                        #27

                        @AliM93
                        Thats the Insert Option part
                        alt text

                        Default its on something else.

                        But no worries.

                        Do you have them in .h with body also like
                        alt text

                        If yes, then click on the function name and go to the refactor menu again ( or press alt+enter)
                        alt text
                        and ask it to move it to .CPP
                        do for both functions.

                        1 Reply Last reply
                        1
                        • A Offline
                          A Offline
                          AliM93
                          wrote on 30 May 2020, 23:27 last edited by AliM93
                          #28

                          @mrjj the code that you wrote me i have to write into .h or .cpp? into the .h i suppose
                          we need my previous code to read the data from csv?

                          M 1 Reply Last reply 30 May 2020, 23:36
                          0
                          • A AliM93
                            30 May 2020, 23:27

                            @mrjj the code that you wrote me i have to write into .h or .cpp? into the .h i suppose
                            we need my previous code to read the data from csv?

                            M Offline
                            M Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on 30 May 2020, 23:36 last edited by
                            #29

                            @AliM93 said in Create a matrix from a .csv file:

                            • the code that you wrote me i have to write into .h or .cpp? into the .h i suppose
                              Yes the DataPoint class goes into .h as shown higher up.

                            we need my previous code to read the data from csv?

                            Yes, this has to be changed to add the values to the
                            DataPoint Data[max_x][max_y];
                            please post it.

                            1 Reply Last reply
                            0
                            • A Offline
                              A Offline
                              AliM93
                              wrote on 30 May 2020, 23:47 last edited by AliM93
                              #30
                                csvModel = new QStandardItemModel(this);
                                csvModel->setColumnCount(3);
                                csvModel->setHorizontalHeaderLabels(QStringList() <<"x" <<y"" << "bool");
                                ui->tableView_1->setModel(csvModel);
                                
                                QFile file("/home/alice/catkin_ws/src/agree_gui/resources/Punti_tappetino.csv");
                                if(!file.open(QFile::ReadOnly | QFile::Text))
                                {
                                  qDebug()<< "FIle not exist";
                                  
                                }
                                else {
                                  QTextStream in(&file);
                                  while (!in.atEnd())
                                  {
                                    QString line = in.readLine();
                                    QList<QStandardItem *>standardItemsList;
                                    for(QString item:line.split(";"))
                                      
                                  }
                                  csvModel ->insertRow(csvModel->rowCount(), standardItemsList);
                                }
                                file.close();
                              ```this is in the .cpp file, and in the .h i put 
                              

                              QStandardItemsModel *csvModel;

                              
                              
                              
                              there are also some include in the cpp : 
                              

                              #include <QTableView>

                              #include <iostream>
                              #include <fstream>
                              #include <sstream>

                              
                              and om the .h 
                              

                              #include <QStandardItemModel>

                              1 Reply Last reply
                              0
                              • M Offline
                                M Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on 30 May 2020, 23:54 last edited by mrjj
                                #31

                                Hi
                                Super
                                Now we add a new function to our new class.
                                lets call it LoadData.

                                add these to the .cpp 
                                #include <QFile>
                                #include <QTextStream>
                                #include <QDebug>
                                
                                // this is changed version of your reader code
                                void MatrixWidget::LoadData()
                                {
                                    QFile file("/home/alice/catkin_ws/src/agree_gui/resources/Punti_tappetino.csv");
                                    if (!file.open(QFile::ReadOnly | QFile::Text)) {
                                        qDebug() << "FIle not exist";
                                
                                    } else {
                                        QTextStream in(&file);
                                        while (!in.atEnd()) {
                                            QString line = in.readLine();
                                            QStringList list = line.split(";");
                                            int x = list.at(0).toInt();
                                            int y = list.at(1).toInt();
                                            bool value = list.at(2).toInt();
                                            if ( x < max_x && y < max_y) // to make sure we dont crash
                                                Data[x][y].value = value; // here we store value
                                            else
                                                qDebug() << "x or y bigger than matrix!";
                                        }
                                
                                    }
                                    file.close();
                                }
                                
                                

                                Do you follow so far ?
                                I have not tried this before so not sure how easy its to follow :)

                                A 1 Reply Last reply 30 May 2020, 23:56
                                1
                                • M mrjj
                                  30 May 2020, 23:54

                                  Hi
                                  Super
                                  Now we add a new function to our new class.
                                  lets call it LoadData.

                                  add these to the .cpp 
                                  #include <QFile>
                                  #include <QTextStream>
                                  #include <QDebug>
                                  
                                  // this is changed version of your reader code
                                  void MatrixWidget::LoadData()
                                  {
                                      QFile file("/home/alice/catkin_ws/src/agree_gui/resources/Punti_tappetino.csv");
                                      if (!file.open(QFile::ReadOnly | QFile::Text)) {
                                          qDebug() << "FIle not exist";
                                  
                                      } else {
                                          QTextStream in(&file);
                                          while (!in.atEnd()) {
                                              QString line = in.readLine();
                                              QStringList list = line.split(";");
                                              int x = list.at(0).toInt();
                                              int y = list.at(1).toInt();
                                              bool value = list.at(2).toInt();
                                              if ( x < max_x && y < max_y) // to make sure we dont crash
                                                  Data[x][y].value = value; // here we store value
                                              else
                                                  qDebug() << "x or y bigger than matrix!";
                                          }
                                  
                                      }
                                      file.close();
                                  }
                                  
                                  

                                  Do you follow so far ?
                                  I have not tried this before so not sure how easy its to follow :)

                                  A Offline
                                  A Offline
                                  AliM93
                                  wrote on 30 May 2020, 23:56 last edited by AliM93
                                  #32

                                  @mrjj yes i understand all the flow! super clear, so far.

                                  M 1 Reply Last reply 31 May 2020, 00:02
                                  0
                                  • A AliM93
                                    30 May 2020, 23:56

                                    @mrjj yes i understand all the flow! super clear, so far.

                                    M Offline
                                    M Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on 31 May 2020, 00:02 last edited by
                                    #33

                                    @AliM93
                                    Super!
                                    Could you open Punti_tappetino.csv in textEditor and copy it to say
                                    https://paste.ofcode.org/
                                    and give me url ?
                                    Then i also have some real data to read it to check loaddata works :)

                                    A 1 Reply Last reply 31 May 2020, 00:04
                                    1
                                    • M mrjj
                                      31 May 2020, 00:02

                                      @AliM93
                                      Super!
                                      Could you open Punti_tappetino.csv in textEditor and copy it to say
                                      https://paste.ofcode.org/
                                      and give me url ?
                                      Then i also have some real data to read it to check loaddata works :)

                                      A Offline
                                      A Offline
                                      AliM93
                                      wrote on 31 May 2020, 00:04 last edited by
                                      #34

                                      @mrjj Done! are you seing it?

                                      M 1 Reply Last reply 31 May 2020, 00:05
                                      0
                                      • A AliM93
                                        31 May 2020, 00:04

                                        @mrjj Done! are you seing it?

                                        M Offline
                                        M Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on 31 May 2020, 00:05 last edited by mrjj
                                        #35

                                        @AliM93
                                        where is the url ?

                                        you have to press paste it and then copy the adress from the top of browser
                                        like
                                        https://paste.ofcode.org/vYJiPZBPqzaDNArjpqPYE7
                                        (just test)

                                        A 1 Reply Last reply 31 May 2020, 00:07
                                        1
                                        • M mrjj
                                          31 May 2020, 00:05

                                          @AliM93
                                          where is the url ?

                                          you have to press paste it and then copy the adress from the top of browser
                                          like
                                          https://paste.ofcode.org/vYJiPZBPqzaDNArjpqPYE7
                                          (just test)

                                          A Offline
                                          A Offline
                                          AliM93
                                          wrote on 31 May 2020, 00:07 last edited by AliM93
                                          #36

                                          @mrjj https://paste.ofcode.org/XLhSxBmaWcaKj2L9CapQ3s

                                          sorry my bad

                                          i have also to declare the function iLoadData in the .h file, right? in the private, protected or in the public slot?

                                          M 1 Reply Last reply 31 May 2020, 00:10
                                          0

                                          26/140

                                          30 May 2020, 23:21

                                          topic:navigator.unread, 114
                                          • Login

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