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
QtWS25 Last Chance

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.
  • S Offline
    S Offline
    SGaist
    Lifetime Qt Champion
    wrote on 12 Nov 2021, 08:38 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
    • S Offline
      S Offline
      SMF-Qt
      wrote on 12 Nov 2021, 09:30 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
      • S Offline
        S Offline
        SMF-Qt
        wrote on 12 Nov 2021, 10:05 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;
        }
        
        J 1 Reply Last reply 12 Nov 2021, 10:10
        0
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 12 Nov 2021, 10:09 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
          • S SMF-Qt
            12 Nov 2021, 10:05

            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;
            }
            
            J Online
            J Online
            JonB
            wrote on 12 Nov 2021, 10:10 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
            • S Offline
              S Offline
              SMF-Qt
              wrote on 12 Nov 2021, 11:39 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

              J 1 Reply Last reply 12 Nov 2021, 11:44
              0
              • S SMF-Qt
                12 Nov 2021, 11:39

                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

                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 12 Nov 2021, 11:44 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
                • S Offline
                  S Offline
                  SMF-Qt
                  wrote on 12 Nov 2021, 12:01 last edited by
                  #14

                  Got it thanks. Problem solved.

                  C 1 Reply Last reply 12 Nov 2021, 12:05
                  1
                  • S SMF-Qt
                    12 Nov 2021, 12:01

                    Got it thanks. Problem solved.

                    C Offline
                    C Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 12 Nov 2021, 12:05 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
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 12 Nov 2021, 20:40 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

                      16/16

                      12 Nov 2021, 20:40

                      • Login

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