Saving Qlist data in a mysql database
-
Hi,
What do you mean by "as a single entity" ? The whole QList as one binary blob ?
If so you can use QDataStream to serialize your list into a QByteArray and store that array as a blob.
-
wrote on 11 Nov 2021, 22:12 last edited by
Yes if possible.
Thanks -
wrote on 12 Nov 2021, 07:16 last edited by
Looking at the documentation QDataStream takes a QIODevice but I see no way to use a QList as a source for the QIODevice?
-
Please take a look at the examples on how to use QDataStream. It clearly shows how to put a QString into a QIODevice (in this case a QFile, but for QByteArray/QBuffer it's the same).
-
Looking at the documentation QDataStream takes a QIODevice but I see no way to use a QList as a source for the QIODevice?
wrote on 12 Nov 2021, 07:39 last edited by JonB 11 Dec 2021, 07:41@SMF-Qt
Be aware that in order to stream yourstruct
to/from aQDataStream
you will need it to implement<<
&>>
operators (not hard, just needs to be done).Note also that the image will be streamed as a PNG image (https://doc.qt.io/qt-5/qimage.html#operator-lt-lt-21), you don't have a choice over this, so that needs to be acceptable to you.
-
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.
11/16