Create a matrix from a .csv file
-
@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. - the code that you wrote me i have to write into .h or .cpp? into the .h i suppose
-
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>
-
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 :) -
@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 :) -
@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) -
@AliM93
nope its not cmakeit 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. };
-
@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 justvoid LoadData();
-
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 changeconst 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 :)
-
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.