[Printing values of a QList of structure]
-
Hello guys,
I hope you're all doing well.So I filled a QList of structure with values but there is something I still don't really understand.
I declared the structure and the QList like this:
typedef struct TelemetryData_s{ QString TimeStamp; QQuaternion quaternion_csv; }TelemetryData_t; QList<TelemetryData_t> Orientation_Data;
Then I tried to fill each field of the QList as follows:
NumberOfTelemetries=0; while(!xin.atEnd()) { line = xin.readLine(); if( line.mid(28,2)=="05" && line.mid(34,4)=="F204") { this->ui->mainwidget->Orientation_Data.append({line.mid(0,21), QQuaternion(toFloat(line.mid(137,8)),toFloat(line.mid(145,8)), toFloat(line.mid(153,8)),toFloat(line.mid(161,8)))}); NumberOfTelemetries++; } }
Then in order to see if the values were added correctly, I used qDebug() as follows:
for(int i=0; i<NumberOfTelemetries;i++) { qDebug()<<"time and date"<<this->ui->mainwidget->Orientation_Data[i].TimeStamp; qDebug()<<"quaternion"<<this->ui->mainwidget->Orientation_Data[i].quaternion_csv; }
And I could see the QList of strucutre was correctly filled.
Now what I don't really get is when I use qDebug inside the while loop and right after filling the QList, without adding a counter, I mean like this:
while(!xin.atEnd()) { line = xin.readLine(); if( line.mid(28,2)=="05" && line.mid(34,4)=="F204") { this->ui->mainwidget->Orientation_Data.append({line.mid(0,21), QQuaternion(toFloat(line.mid(137,8)),toFloat(line.mid(145,8)), toFloat(line.mid(153,8)),toFloat(line.mid(161,8)))}); NumberOfTelemetries++; qDebug()<<"time and date"<<this->ui->mainwidget->Orientation_Data.TimeStamp; qDebug()<<"quaternion"<<this->ui->mainwidget->Orientation_Data.quaternion_csv;
I get an error which is "no member named TimeStamp in 'QList<telemetry_data>'.
I don't understand why when I add a counter everything works fine but when I don't I get that error. Why doesn't the program recognize the member TimeStamp ? is it because there are more than ONE TimeStamp value in the QList of structure ? If that's the reason, then why the error can't be something related to the QList indices ? because my structure DOES have a member named TimeStamp.
I'm really sorry for posting such a question, I know the answer to this may be so obvious but I need to make sure I got it all right.
Thank you all for helping me.
Have a nice day. -
@appdev said in [Printing values of a QList of structure]:
is it because there are more than ONE TimeStamp value in the QList of structure ?
no, its because QList has no member named TimeStamp, only the struct objects inside the list has that.
So you have to specify what element of your list you want to address, before you can get the timestamp valuein your case here, 2 options:
//option 1 if( line.mid(28,2)=="05" && line.mid(34,4)=="F204") { this->ui->mainwidget->Orientation_Data.append({line.mid(0,21), QQuaternion(toFloat(line.mid(137,8)),toFloat(line.mid(145,8)), toFloat(line.mid(153,8)),toFloat(line.mid(161,8)))}); NumberOfTelemetries++; qDebug()<<"time and date"<<this->ui->mainwidget->Orientation_Data.last().TimeStamp; qDebug()<<"quaternion"<<this->ui->mainwidget->Orientation_Data.last().quaternion_csv;
last() returns the object at the end of the list, which is the one you recently appended
https://doc.qt.io/qt-5/qlist.html#last//option 2 if( line.mid(28,2)=="05" && line.mid(34,4)=="F204") { TelemetryData_s tData = {line.mid(0,21), QQuaternion(toFloat(line.mid(137,8)),toFloat(line.mid(145,8)), toFloat(line.mid(153,8)),toFloat(line.mid(161,8)))}; qDebug()<<"time and date"<<tData.TimeStamp; qDebug()<<"quaternion"<<tData.quaternion_csv; this->ui->mainwidget->Orientation_Data.append(tData); NumberOfTelemetries++;
you create a local, temporary object of TelemetryData_s, inspect that, and append it than to the list