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 52.6k 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 Offline
    A Offline
    AliM93
    wrote on 31 May 2020, 02:00 last edited by
    #115

    Build! thansk

    1 Reply Last reply
    0
    • A Offline
      A Offline
      AliM93
      wrote on 31 May 2020, 02:01 last edited by
      #116

      i run it but obviouslt i can't see anything

      1 Reply Last reply
      0
      • A Offline
        A Offline
        AliM93
        wrote on 31 May 2020, 02:02 last edited by
        #117

        because we have to add the matrix in the widget, right?

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

          because we have to add the matrix in the widget, right?

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

          @AliM93
          yes.
          You should still have the layout from before, correct ?
          then in Grids constrcutor

          GridDialog::GridDialog(QWidget *parent) :
              QDialog(parent),
              ui(new Ui::GridDialog)
          {
              ui->setupUi(this);
          
              MatrixWidget * m = new MatrixWidget (this);
              ui->verticalLayout->addWidget(m); /// you name might differ for layout
          
          }
          

          We dont need the plain widget any more. you can delete it. we use the layout

          1 Reply Last reply
          0
          • A Offline
            A Offline
            AliM93
            wrote on 31 May 2020, 02:08 last edited by
            #119

            done! but i only see the grid, not the content

            M 1 Reply Last reply 31 May 2020, 02:09
            0
            • A AliM93
              31 May 2020, 02:08

              done! but i only see the grid, not the content

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

              @AliM93
              \o/
              Good work.
              I hope you learn a few Creator tricks.

              we didnt paint the values yet :)
              just all other stuff :=)

              But its 4 in the morning so rest must be tomorrow as its bed time for me :)

              A 1 Reply Last reply 31 May 2020, 02:10
              0
              • M mrjj
                31 May 2020, 02:09

                @AliM93
                \o/
                Good work.
                I hope you learn a few Creator tricks.

                we didnt paint the values yet :)
                just all other stuff :=)

                But its 4 in the morning so rest must be tomorrow as its bed time for me :)

                A Offline
                A Offline
                AliM93
                wrote on 31 May 2020, 02:10 last edited by
                #121

                @mrjj yes! but i don't even see the number

                M 1 Reply Last reply 31 May 2020, 02:11
                0
                • A Offline
                  A Offline
                  AliM93
                  wrote on 31 May 2020, 02:10 last edited by
                  #122

                  anyway, ok for tomorrow, you have been too much patient with me, thanks. and for the first time, i can say thay i better undestand how qt works! i start to appreciate it

                  1 Reply Last reply
                  0
                  • A AliM93
                    31 May 2020, 02:10

                    @mrjj yes! but i don't even see the number

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

                    @AliM93
                    well do you have that version of paint ?

                    
                    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
                        bw = w / max_x;
                        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  ) )  ;
                                p.drawText(QRect( xi * bw, yi * bh, bw, bh  ),
                                           QString::number(xi + 1) + "," + QString::number(yi+1) ); // the +1 aswe dont want to use first at 0,0
                            }
                        }
                    
                    }
                    
                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      AliM93
                      wrote on 31 May 2020, 02:11 last edited by
                      #124

                      yes i have

                      M 1 Reply Last reply 31 May 2020, 02:12
                      0
                      • A AliM93
                        31 May 2020, 02:11

                        yes i have

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

                        @AliM93
                        But it dont draw numbers ?
                        That seems very odd.

                        But you do see grid ?

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          AliM93
                          wrote on 31 May 2020, 02:13 last edited by
                          #126

                          sorry! now i have, i lost a part of that version of matrix, sorry!

                          M 1 Reply Last reply 31 May 2020, 02:14
                          0
                          • A AliM93
                            31 May 2020, 02:13

                            sorry! now i have, i lost a part of that version of matrix, sorry!

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

                            @AliM93
                            Ok that good news as it would be very impossible that numbers went missing :)

                            what is remaining is the click logic and to actual draw the 1.

                            how shall we draw 0 ones ?
                            just no value in cell and cant be selected or what is the goal?

                            1 Reply Last reply
                            0
                            • A Offline
                              A Offline
                              AliM93
                              wrote on 31 May 2020, 02:16 last edited by
                              #128

                              my idea was to color all the 1s in green and the 0s in gray. so the user know that can select only the green cells, is it possible?

                              1 Reply Last reply
                              0
                              • A Offline
                                A Offline
                                AliM93
                                wrote on 31 May 2020, 02:17 last edited by
                                #129

                                i don't want to disturb you anymore tonight, if you want and if you have time we can continue tomorrow

                                1 Reply Last reply
                                0
                                • M Offline
                                  M Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on 31 May 2020, 02:21 last edited by
                                  #130

                                  Yes its possible. we can paint it any way we like. teasers included.

                                  Well im getting very tired as 4.20 in the morning is late for me even i do love programming at night.

                                  Tomorrow we add the last part. It's not that much. Promise.
                                  You did good. First custom widget is always a bit tuff.

                                  alt text

                                  alt text

                                  1 Reply Last reply
                                  0
                                  • A Offline
                                    A Offline
                                    AliM93
                                    wrote on 31 May 2020, 02:23 last edited by
                                    #131

                                    thank you so much! you help me a lot also in understand the ideas. thanks. night!

                                    1 Reply Last reply
                                    0
                                    • M Offline
                                      M Offline
                                      mrjj
                                      Lifetime Qt Champion
                                      wrote on 31 May 2020, 11:20 last edited by mrjj
                                      #132

                                      Hi
                                      The last remaining steps are to
                                      +Add box width/height as members to use when clicking and a variable to keep track of the number of selected

                                      class MatrixWidget : public QWidget
                                      {....    
                                          int bw =0;
                                          int bh =0;
                                          int selCount=0; // to make sure we can only select 3
                                      ,...
                                      

                                      +call loadData from construtor.

                                      +Set some colors in void MatrixWidget::LoadData()
                                      for 0 cells and 1 cells (see code)

                                      +Add selection logic to mousePress

                                      void MatrixWidget::mousePressEvent(QMouseEvent *event)
                                      {
                                          QPoint p = event->pos(); // where we clicked
                                      
                                          int xindex =  p.x() / bw;
                                          int yindex =  p.y() / bh;
                                      
                                          qDebug() << xindex << " " << yindex;
                                          // get the data point ( we should check index is not outside array...)
                                          DataPoint &dp = Data[xindex + 1 ][yindex + 1]; 
                                      
                                          // value zero cannot be selected
                                          if ( dp.value == 0 ) return;
                                      
                                          // selection logic. If not already selected, select it and set color. Else deselect and reset color
                                          if ( dp.isSelected == false) {
                                      
                                              // no more than 3 selected ( 0,1,2 )
                                              if ( selCount > 2 ) return;
                                      
                                              dp.DrawColor = Qt::blue;
                                              dp.isSelected = true;
                                              selCount++; // extra selected
                                          } else {
                                              dp.DrawColor =  Qt::green;
                                              dp.isSelected = false;
                                              selCount--; // less selected
                                          }
                                          update(); // tell it to redraw grid
                                      
                                      }
                                      
                                      

                                      +Adjust draw to skip 0,0 as we don't use it but the array must start at zero.

                                      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
                                          bw = w / max_x;
                                          bh = h / max_y;
                                      
                                          // now we loop and draw the boxes. we dont use the boxes from 0,0 as data starts with 1,1
                                          for (int xi = 0; xi < max_x - 1; ++xi) {
                                              for (int yi = 0; yi < max_x - 1; ++yi) {
                                                  p.setBrush(QBrush( Data[xi + 1][yi + 1].DrawColor )); // +1 as we dont want to use from 0,0
                                                  QRect cellRect( xi * bw, yi * bh, bw, bh  );
                                                  p.drawRect( cellRect );
                                                  // show x,y . just for debug
                                                  p.drawText( cellRect ,
                                                             QString::number(xi + 1) + "," +
                                                             QString::number(yi +1) );
                                      
                                              }
                                          }
                                      }
                                      
                                      
                                      

                                      Then we have a working version.

                                      alt text

                                      project
                                      https://www.dropbox.com/s/z4nllgag1tjy4j3/GridWidgetApp.zip?dl=0

                                      Things to improve/add
                                      The limit of how many points user can select,
                                      should be a parameter to the class so it can be set to anything and not a fixed value.

                                      The colors could also be parameters ( for not selectable, selectable and selected)

                                      Add a function to return the selected data pointsby looping the Data list and check isSelected.

                                      1 Reply Last reply
                                      0
                                      • A Offline
                                        A Offline
                                        AliM93
                                        wrote on 31 May 2020, 11:59 last edited by AliM93
                                        #133

                                        thanks! i'll do it step by step, i quite understand it, but when i run i got a black grid, and the error floating point exeption (core dumped) No more error, (i also see the coordinates in the debug), but black grid.

                                        M 1 Reply Last reply 31 May 2020, 12:06
                                        0
                                        • A AliM93
                                          31 May 2020, 11:59

                                          thanks! i'll do it step by step, i quite understand it, but when i run i got a black grid, and the error floating point exeption (core dumped) No more error, (i also see the coordinates in the debug), but black grid.

                                          M Offline
                                          M Offline
                                          mrjj
                                          Lifetime Qt Champion
                                          wrote on 31 May 2020, 12:06 last edited by
                                          #134

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

                                          it, but when i run i got a black grid, and the error floating point exeption (core dumped)

                                          In my project or yours ?

                                          A 1 Reply Last reply 31 May 2020, 12:06
                                          0

                                          124/140

                                          31 May 2020, 02:11

                                          • Login

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