Create a matrix from a .csv file
-
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. -
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 selectedclass 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.
project
https://www.dropbox.com/s/z4nllgag1tjy4j3/GridWidgetApp.zip?dl=0Things 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.
-
@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 ?
-