PDA

View Full Version : QBytearray output in single line ?



benver
13th April 2020, 21:41
Hi the board

Sorry if my english is not fluently ( im just French and im doing my best ;) )

I'm really a newbie about C++ and QT just started it few days ago. Ive create a small progarm to get data from Arduino serial port, i got the data, i can parse the data as i wanted to do
And i even save the data into csv file

Now i have made some code to access a specific row in the file, it works i can display on textEdit, but i need to get a code to get maximum value of this specific row

I try many code but QT debug give me reply line by line and the function std::max_element cant works correctly.... i would like to get output in one line and then execute max_element

Ive been searching everyboard for hours, but i cant solved it alone


void Dialog::on_pushButton_clicked()
{
{


QString fichier2= "c:/qt/essaiqt.csv";
QFile file(fichier2);
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
while (!file.atEnd())

{

QByteArray line = file.readLine();
QByteArrayList fields {line.split(';')};
QByteArray column2 {(fields [3])};
QDataStream s(column2);
fields[3] = fields [3].simplified();
column2 = column2.simplified();
column2.toDouble();
QVector<double> essai;


auto valeur = std::max_element(column2.begin(), column2.end());
std::cout << "max "<< valeur;
qDebug() << "x=" << column2;

{


ui->textEdit->append(fields[3]);


}
}
}


and output looks like it 13380

Your help will be highly appreciate :cool:

ChrisW67
14th April 2020, 01:14
Your English is far better than my French :)

I do not think your code will compile (e.g. there is no simplified() method in QDataStream and there does not seem to be a closing brace on the while() loop)

What does "c:/qt/essaiqt.csv" contain? Something like this perhaps?
1;3359;3;4;5
1;9952;3;4;5
1;69855;3;4;5
1;3524;3;4;9999
What are you trying to achieve? For each line, output the largest value in the line (3359, 9952, 69855, 9999), or
Output the largest value found in "column2" of any line (69855)

benver
14th April 2020, 09:55
Your English is far better than my French :)

I do not think your code will compile (e.g. there is no simplified() method in QDataStream and there does not seem to be a closing brace on the while() loop)

What does "c:/qt/essaiqt.csv" contain? Something like this perhaps?
1;3359;3;4;5
1;9952;3;4;5
1;69855;3;4;5
1;3524;3;4;9999
What are you trying to achieve? For each line, output the largest value in the line (3359, 9952, 69855, 9999), or
Output the largest value found in "column2" of any line (69855)

Thanks for reply
I would like to put it in one line and find maximum, because if i'm searching maximum on not complete line, it will give me return of each max of each line, thats why i would like to get first on one line then apply minmax et max_element

Lesiok
14th April 2020, 10:39
Can you show a real CSV file (several lines) and answer directly the question what does "maximum value" mean (maximum per line, column or file)? In my opinion, the description of your problem and your answers are a bit chaotic.

benver
14th April 2020, 12:22
Can you show a real CSV file (several lines) and answer directly the question what does "maximum value" mean (maximum per line, column or file)? In my opinion, the description of your problem and your answers are a bit chaotic.

serial data coming on csv file looks like it below. So i get the data of only on row with success. But i want to compare all the row i save for example row C, and get the maximum value in it

13381

The problem with my code is that it compares line to line, comparing C1 with C1 and C2 with C2 because Array coming line after line i would like to compare from C1 to CXX and find max

Hope i have explained it better now :D

Lesiok
14th April 2020, 13:27
First of all : C is a column not a row. The cell C3 lies in column C of row 3. If You want to find max value from column You can do something like this :
void Dialog::on_pushButton_clicked()
{
QString fichier2= "c:/qt/essaiqt.csv";
QFile file(fichier2);
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
double max_in_column = 0;
bool first_line = true;
while (!file.atEnd())
{
QByteArray line = file.readLine();
QByteArrayList fields{line.split(';')};
QString column2(fields[3])
if(first_line || column2.toDouble() > max_in_column)
max_in_column = column2.toDouble();
first_line = false;
}
//here in max_in_column You have max value in column C
}Code not checked invented quickly.

benver
14th April 2020, 16:22
First of all : C is a column not a row. The cell C3 lies in column C of row 3. If You want to find max value from column You can do something like this :[CODE]void .

MANY MANY THANKS !! it works perfectly !! Nice to be helped so fast and so efficient :cool: