Solved QVector serialization / deserialization
-
Serialization is ok ;
deserialization gives below error:
/Users/alpullu/Qt/5.11.0/clang_64/lib/QtCore.framework/Headers/qdatastream.h:243: error: use of overloaded operator '>>' is ambiguous (with operand types 'QDataStream' and 'typename QVector<enrolment_>::value_type' (aka 'enrolment_')) s >> t; ~ ^ ~
// load. MUST BE SAME ORDER as when saved QDataStream& operator >>(QDataStream& in, enrolment_& Entry) { in >> Entry.person_name; in >> Entry.person_foto; in >> Entry.category; in >> Entry.face_indexed_status; return in; }
-
Please always show code as you being unused to Qt / c++ might
give situation where you did something in code we would never think of. -
I exactly copied your deserialization example :
// load. MUST BE SAME ORDER as when saved QDataStream& operator >>(QDataStream& in, enrolment_& Entry) { in >> Entry.person_name; in >> Entry.person_foto; in >> Entry.category; in >> Entry.face_indexed_status; return in; } honestly didnt understand why its not working . :(
-
@RahibeMeryem
hi
but does the error point to this ?
I assumed it was where you used it.
as in where you try to load in the file again.
Can you show that code ? -
the error is in this line : in_personelVec >> enrolment_Db;
gives:
/Users/xx/Qt/5.11.0/clang_64/lib/QtCore.framework/Headers/qdatastream.h:243: error: use of overloaded operator '>>' is ambiguous (with operand types 'QDataStream' and 'typename QVector<enrolment_>::value_type' (aka 'enrolment_'))
s >> t;
~ ^ ~void Widget::load_enrollment_if_exist() { bool fileExists = QFileInfo::exists(enrollment_path) && QFileInfo(enrollment_path).isFile(); if (fileExists){ QFile personel_file(enrollment_path); personel_file.open(QIODevice::ReadOnly); QDataStream in_personelVec(&personel_file); // we will deserialize the data into the file in_personelVec.setVersion(QDataStream::Qt_5_10); in_personelVec >> enrolment_Db; ///////////THE ERROR is HERE qDebug() << " enrolment yüklendi: , personel sayısı: " << enrolment_Db.size() ; } }
// save QDataStream& operator <<(QDataStream& out, const enrolment_ Entry) { out << Entry.person_name; out << Entry.person_foto; out << Entry.category; out << Entry.face_indexed_status; return out; } // load. MUST BE SAME ORDER as when saved QDataStream& operator >>(QDataStream& in, enrolment_& Entry) { in >> Entry.person_name; in >> Entry.person_foto; in >> Entry.category; in >> Entry.face_indexed_status; return in; }
-
Do we need to write QVector<enrolment_> size ? because it has 1000 elements ?
-
What type is
Entry.face_indexed_status
?No you don't have to do anything, QVector is already a supported type so as long you provide the data stream operator for your class you're good.
Note that again, you have to handle that properly in your code, meaning that if you stream something, then your vector then something else, in your read code you have to again do that in the same order.
-
in the header :
struct enrolment_ {
QString person_name;
QPixmap person_foto;
QString category;
QString file;
QString face_indexed_status;
};QDataStream& operator <<(QDataStream& out, const enrolment_ Entry);
QDataStream& operator >>(QDataStream& in, enrolment_ Entry); -
Did you add the missing & to BOTH .h and cpp ?
seems to be missing in .h
QDataStream& operator >>(QDataStream& in, enrolment_ & Entry); // NOTE the &please check u changed in both places.
-
Finally I figured it out ... to find that I missed '&' . in .h . took half day with 250 litre coffee :)))
But I want to thanks you all Gentlemen...
Really this is the best and useful answer for me all my Qt adventure
THANKS
THANKS..
-
@RahibeMeryem
Super!
The good part being - this is the way for all classes you make.
So you now can stream anything you like. -
Now I have implemented with QList struct, a issue is QDataStream only support for me QString values otherwise (with numbers) only read a part of information. this works for me.
QDataStream& operator << (QDataStream& out, const anualxls Entry) {
out << QString::number(Entry.anio);
out << QString::number(Entry.suma,'f');
out << QString::number(Entry.sumawo,'f' );
out << QString::number(Entry.tasa, 'f') ;
out << QString::number(Entry.tasawo, 'f');qDebug() << "Escribiendo " << Entry.anio << Entry.suma << Entry.tasa;
return out;
}// load. MUST BE SAME ORDER as when saved
QDataStream& operator >>(QDataStream& in, anualxls &Entry) {
QString temp;
in >> temp; Entry.anio = temp.toInt();
in >> temp; Entry.suma = temp.toFloat();
in >> temp; Entry.sumawo = temp.toFloat();
in >> temp; Entry.tasa = temp.toFloat();
in >> temp; Entry.tasawo = temp.toFloat();
qDebug() << "Leyendo " << Entry.anio << Entry.suma << Entry.tasa;
return in;
} -
@eangeli__ said in QVector serialization / deserialization:
a issue is QDataStream only support for me QString values otherwise (with numbers) only read a part of informatio
How did you do it with numbers? It should work.