Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Brainstorm
  4. Saving Qlist data in a mysql database

Saving Qlist data in a mysql database

Scheduled Pinned Locked Moved Unsolved Brainstorm
16 Posts 5 Posters 1.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SMF-QtS Offline
    SMF-QtS Offline
    SMF-Qt
    wrote on last edited by
    #1

    This is my data setup (simplified):

    Class Animation
    {
    ...
    typedef struct
    {
    QImage I;
    QTime ts;
    } Anim_t;
    ...
    };

    QList<Animation::Anim_t> Animations;

    The QList can have between 0 and 10 items in it at any one time.
    The QImage's are small 512x512 pixels.
    I would like to save the whole Qlist in a mysql table as a single entity.
    Has anyone got a suggested solution for this problem ?

    Thanks

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • SMF-QtS Offline
        SMF-QtS Offline
        SMF-Qt
        wrote on last edited by
        #3

        Yes if possible.
        Thanks

        1 Reply Last reply
        0
        • SMF-QtS Offline
          SMF-QtS Offline
          SMF-Qt
          wrote on last edited by
          #4

          Looking at the documentation QDataStream takes a QIODevice but I see no way to use a QList as a source for the QIODevice?

          JonBJ 1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            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).

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            2
            • SMF-QtS SMF-Qt

              Looking at the documentation QDataStream takes a QIODevice but I see no way to use a QList as a source for the QIODevice?

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @SMF-Qt
              Be aware that in order to stream your struct to/from a QDataStream 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.

              1 Reply Last reply
              1
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                In addition to @JonB, the operators shall be implemented for your custom structure, not QList.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1
                • SMF-QtS Offline
                  SMF-QtS Offline
                  SMF-Qt
                  wrote on last edited by
                  #8

                  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)

                  1 Reply Last reply
                  0
                  • SMF-QtS Offline
                    SMF-QtS Offline
                    SMF-Qt
                    wrote on last edited by
                    #9

                    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;
                    }
                    
                    JonBJ 1 Reply Last reply
                    0
                    • Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      It must be a free function, if it's a member of your struct then you have to remove const Anim_t &anim

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      1 Reply Last reply
                      3
                      • SMF-QtS SMF-Qt

                        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;
                        }
                        
                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by
                        #11

                        @SMF-Qt
                        (In addition to @Christian-Ehrlicher) I thought you were going to implementing the << & >> streaming operators for your Anim_t struct itself.

                        1 Reply Last reply
                        0
                        • SMF-QtS Offline
                          SMF-QtS Offline
                          SMF-Qt
                          wrote on last edited by
                          #12

                          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

                          jsulmJ 1 Reply Last reply
                          0
                          • SMF-QtS SMF-Qt

                            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

                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            @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."

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            3
                            • SMF-QtS Offline
                              SMF-QtS Offline
                              SMF-Qt
                              wrote on last edited by
                              #14

                              Got it thanks. Problem solved.

                              Christian EhrlicherC 1 Reply Last reply
                              1
                              • SMF-QtS SMF-Qt

                                Got it thanks. Problem solved.

                                Christian EhrlicherC Offline
                                Christian EhrlicherC Offline
                                Christian Ehrlicher
                                Lifetime Qt Champion
                                wrote on last edited by
                                #15

                                @SMF-Qt Then also please mark this topic as solved, thx.

                                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                Visit the Qt Academy at https://academy.qt.io/catalog

                                1 Reply Last reply
                                0
                                • SGaistS Offline
                                  SGaistS Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  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.

                                  Interested in AI ? www.idiap.ch
                                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  1 Reply Last reply
                                  1

                                  • Login

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • Users
                                  • Groups
                                  • Search
                                  • Get Qt Extensions
                                  • Unsolved