Saving Qlist data in a mysql database
-
In addition to @JonB, the operators shall be implemented for your custom structure, not QList.
-
wrote on 12 Nov 2021, 09:30 last edited by
Tried this for the << operator:
QDataStream &operator<<(QDataStream &stream,const Anim_t &anim) { stream << anim.ts.msecsSinceStartOfDay() << anim.I; return stream; }
Got this error, what have I missed ?
anim.h:35:15: error: 'QDataStream& Anim::operator<<(QDataStream&, const Anim::Anim_t&)' must have exactly one argument
35 | QDataStream &operator<<(QDataStream &stream,const Anim_t &anim) -
wrote on 12 Nov 2021, 10:05 last edited by
For completeness I have done this for the ">>" operator and this gives a similar error:
QDataStream &operator>>(QDataStream &stream,const Anim_t &anim) { int t; stream >> t >> anim.I; anim.ts = QTime::fromMSecsSinceStartOfDay(t); return stream; }
-
It must be a free function, if it's a member of your struct then you have to remove
const Anim_t &anim
-
For completeness I have done this for the ">>" operator and this gives a similar error:
QDataStream &operator>>(QDataStream &stream,const Anim_t &anim) { int t; stream >> t >> anim.I; anim.ts = QTime::fromMSecsSinceStartOfDay(t); return stream; }
wrote on 12 Nov 2021, 10:10 last edited by@SMF-Qt
(In addition to @Christian-Ehrlicher) I thought you were going to implementing the<<
&>>
streaming operators for yourAnim_t struct
itself. -
wrote on 12 Nov 2021, 11:39 last edited by
Ok implimented operators as free functions and everything now compiles, however on running the application the QByteArray is empty (example with one element in QList):
...
QDataStream &operator<<(QDataStream &stream, const Anim::Anim_t &anim)
{
stream << anim.ts.msecsSinceStartOfDay() << anim.I;
return stream;
}QDataStream &operator>>(QDataStream &stream, Anim::Anim_t &anim)
{
int t;
stream >> t >> anim.I;
anim.ts = QTime::fromMSecsSinceStartOfDay(t);
return stream;
}...
QByteArray array; QDataStream stream(array); for(int i=0;i<Animations.size();i++) { stream << Animations.at(i); } QByteArray tmp=qCompress(array,9); fprintf(stderr,"Binary Blob Animations (%lld) size %lld compressed size %lld\n",Animations.size(),array.size(),tmp.size());
...
Binary Blob Animations (1) size 0 compressed size 4
Any suggestions ?
Thanks -
Ok implimented operators as free functions and everything now compiles, however on running the application the QByteArray is empty (example with one element in QList):
...
QDataStream &operator<<(QDataStream &stream, const Anim::Anim_t &anim)
{
stream << anim.ts.msecsSinceStartOfDay() << anim.I;
return stream;
}QDataStream &operator>>(QDataStream &stream, Anim::Anim_t &anim)
{
int t;
stream >> t >> anim.I;
anim.ts = QTime::fromMSecsSinceStartOfDay(t);
return stream;
}...
QByteArray array; QDataStream stream(array); for(int i=0;i<Animations.size();i++) { stream << Animations.at(i); } QByteArray tmp=qCompress(array,9); fprintf(stderr,"Binary Blob Animations (%lld) size %lld compressed size %lld\n",Animations.size(),array.size(),tmp.size());
...
Binary Blob Animations (1) size 0 compressed size 4
Any suggestions ?
Thanks@SMF-Qt said in Saving Qlist data in a mysql database:
QDataStream stream(array);
Please read: https://doc.qt.io/qt-5/qdatastream.html#QDataStream-3
"Use QDataStream(QByteArray*, int) if you want to write to a byte array." -
wrote on 12 Nov 2021, 12:01 last edited by
Got it thanks. Problem solved.
-
@SMF-Qt Then also please mark this topic as solved, thx.
-
If I may, there's no need for the loop, you should be able to directly stream the list object and not go manually with that loop.
16/16