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 53.0k 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.
  • 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
            • A AliM93
              31 May 2020, 00:07

              @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 Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 31 May 2020, 00:10 last edited by
              #37

              @AliM93
              ah better :)
              yes also add to public section.
              we might call it from outside so its public.

              A 1 Reply Last reply 31 May 2020, 00:12
              1
              • M mrjj
                31 May 2020, 00:10

                @AliM93
                ah better :)
                yes also add to public section.
                we might call it from outside so its public.

                A Offline
                A Offline
                AliM93
                wrote on 31 May 2020, 00:12 last edited by
                #38

                @mrjj ok! but i get some trouble with the struct. invalid use of non-static data member.

                M 1 Reply Last reply 31 May 2020, 00:15
                0
                • A AliM93
                  31 May 2020, 00:12

                  @mrjj ok! but i get some trouble with the struct. invalid use of non-static data member.

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

                  @AliM93
                  can you show line where it says that ´
                  Mine compiles fine.

                  1 Reply Last reply
                  1
                  • A Offline
                    A Offline
                    AliM93
                    wrote on 31 May 2020, 00:17 last edited by AliM93
                    #40
                    This post is deleted!
                    M 1 Reply Last reply 31 May 2020, 00:22
                    0
                    • A AliM93
                      31 May 2020, 00:17

                      This post is deleted!

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

                      @AliM93
                      nope its not cmake

                      it seems you forgot the ending of the DataPoint class ?
                      the }; part ?

                      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.
                      };
                      
                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        AliM93
                        wrote on 31 May 2020, 00:24 last edited by
                        #42

                        sorry.
                        solved. and now in public slot :
                        i add : void::Matrixwidget LoadData?

                        M 1 Reply Last reply 31 May 2020, 00:27
                        0
                        • A AliM93
                          31 May 2020, 00:24

                          sorry.
                          solved. and now in public slot :
                          i add : void::Matrixwidget LoadData?

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

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

                          void::Matrixwidget LoadData?

                          yes but mind the use of ::
                          its only use outside class so in .h its just

                          void LoadData();

                          1 Reply Last reply
                          1
                          • A Offline
                            A Offline
                            AliM93
                            wrote on 31 May 2020, 00:28 last edited by AliM93
                            #44

                            done! thanks, now we have to modify the paintevent in order to show the grid, right?

                            M 1 Reply Last reply 31 May 2020, 00:30
                            1
                            • A AliM93
                              31 May 2020, 00:28

                              done! thanks, now we have to modify the paintevent in order to show the grid, right?

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

                              @AliM93

                              Ok, small fix.
                              You data has max x,y as
                              x: 14 y: 21
                              so our array is one to small as we start in 0 (zero)
                              so change

                              const int max_x = 14;
                              const int max_y = 21;

                              to

                              const int max_x = 15;
                              const int max_y = 22;

                              as else we drop some :)

                              A 1 Reply Last reply 31 May 2020, 00:32
                              1
                              • M mrjj
                                31 May 2020, 00:30

                                @AliM93

                                Ok, small fix.
                                You data has max x,y as
                                x: 14 y: 21
                                so our array is one to small as we start in 0 (zero)
                                so change

                                const int max_x = 14;
                                const int max_y = 21;

                                to

                                const int max_x = 15;
                                const int max_y = 22;

                                as else we drop some :)

                                A Offline
                                A Offline
                                AliM93
                                wrote on 31 May 2020, 00:32 last edited by
                                #46

                                @mrjj ok thanks

                                1 Reply Last reply
                                0
                                • M Offline
                                  M Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on 31 May 2020, 00:35 last edited by
                                  #47

                                  Ok next step is to add some paint code.

                                  So in .cpp

                                  void MatrixWidget::paintEvent(QPaintEvent *event)
                                  {
                                  
                                      QPainter p(this);
                                      // draw frame.
                                      p.drawRect(0, 0, width() - 1, height() - 1);
                                  
                                      // size of area we have. w = width , h = height , we take 2 pixles for border
                                      int w = width() - 2;
                                      int h = height() - 2;
                                  
                                      // now we find out how big each box should be which area we have  divided with how many on x and y
                                      int bw = w / max_x;
                                      int bh = h / max_y;
                                  
                                      // now we loop and drw the boxes
                                      for (int xi = 0; xi < max_x; ++xi) {
                                          for (int yi = 0; yi < max_x; ++yi) {
                                              p.drawRect( QRect( xi * bw, yi * bh, bw, bh  ) )  ;
                                          }
                                      }
                                  
                                  }
                                  

                                  and then next we will add to some UI so we can see what it start working :)
                                  Teaser.
                                  alt text

                                  A 1 Reply Last reply 31 May 2020, 00:40
                                  0
                                  • M mrjj
                                    31 May 2020, 00:35

                                    Ok next step is to add some paint code.

                                    So in .cpp

                                    void MatrixWidget::paintEvent(QPaintEvent *event)
                                    {
                                    
                                        QPainter p(this);
                                        // draw frame.
                                        p.drawRect(0, 0, width() - 1, height() - 1);
                                    
                                        // size of area we have. w = width , h = height , we take 2 pixles for border
                                        int w = width() - 2;
                                        int h = height() - 2;
                                    
                                        // now we find out how big each box should be which area we have  divided with how many on x and y
                                        int bw = w / max_x;
                                        int bh = h / max_y;
                                    
                                        // now we loop and drw the boxes
                                        for (int xi = 0; xi < max_x; ++xi) {
                                            for (int yi = 0; yi < max_x; ++yi) {
                                                p.drawRect( QRect( xi * bw, yi * bh, bw, bh  ) )  ;
                                            }
                                        }
                                    
                                    }
                                    

                                    and then next we will add to some UI so we can see what it start working :)
                                    Teaser.
                                    alt text

                                    A Offline
                                    A Offline
                                    AliM93
                                    wrote on 31 May 2020, 00:40 last edited by AliM93
                                    #48

                                    @mrjj thanks also for comments, so i can better follow the flow. but sorry i have to include QPainter somewhere? yes solved!

                                    M 1 Reply Last reply 31 May 2020, 00:43
                                    1
                                    • A AliM93
                                      31 May 2020, 00:40

                                      @mrjj thanks also for comments, so i can better follow the flow. but sorry i have to include QPainter somewhere? yes solved!

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

                                      @AliM93
                                      Yep, need includes.

                                      I hope comments are enough to understand it.
                                      its just calculating how big each box should when we have max_x and max_y and some area ( the widgets area)

                                      Shall we try to add it and UI and see something?
                                      I want to show you a Creator feature called Promotion that makes it easy to use such custom control.

                                      (note i bumped your upvotes so you should be able to post faster now)

                                      A 1 Reply Last reply 31 May 2020, 00:44
                                      0
                                      • M mrjj
                                        31 May 2020, 00:43

                                        @AliM93
                                        Yep, need includes.

                                        I hope comments are enough to understand it.
                                        its just calculating how big each box should when we have max_x and max_y and some area ( the widgets area)

                                        Shall we try to add it and UI and see something?
                                        I want to show you a Creator feature called Promotion that makes it easy to use such custom control.

                                        (note i bumped your upvotes so you should be able to post faster now)

                                        A Offline
                                        A Offline
                                        AliM93
                                        wrote on 31 May 2020, 00:44 last edited by
                                        #50

                                        @mrjj sure! the only thing is, as you can image from my images, i'm working into a bigger file, and when i run the app i have also a main_window.ui. so i have to link all the things to that. is it possilbe, isn'it it?

                                        M 1 Reply Last reply 31 May 2020, 00:45
                                        0
                                        • A AliM93
                                          31 May 2020, 00:44

                                          @mrjj sure! the only thing is, as you can image from my images, i'm working into a bigger file, and when i run the app i have also a main_window.ui. so i have to link all the things to that. is it possilbe, isn'it it?

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

                                          @AliM93
                                          yes, i though we add it to your main ui to see its working ?

                                          Its same project still, correct ?

                                          A 1 Reply Last reply 31 May 2020, 00:48
                                          0

                                          41/140

                                          31 May 2020, 00:22

                                          • Login

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