Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Simple custom QVariant serialization not working
Forum Updated to NodeBB v4.3 + New Features

Simple custom QVariant serialization not working

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 2 Posters 813 Views 1 Watching
  • 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.
  • P Offline
    P Offline
    Per Ivar Bruheim
    wrote on last edited by
    #1

    I have implemented a custom QVariant type by registering the class with Q_DECLARE_METATYPE(Vector) and defining the stream operators.

    Vector.h:

    class Vector
    {
    public:
       Vector();
       QString mFoo;
    };
    
    Q_DECLARE_METATYPE(Vector)
    QDataStream& operator<<(QDataStream& stream, const Vector& v);
    QDataStream& operator>>(QDataStream& stream, Vector& v);
    

    Vector.cpp:

    #include "Vector.h"
    
    Vector::Vector()
    { }
    
    QDataStream& operator<<(QDataStream& stream, const GVector& v)
    {
       stream << v.mFoo;
       return stream;
    }
    
    QDataStream& operator>>(QDataStream& stream, Vector& v)
    {
       stream >> v.mFoo;
       return stream;
    }
    

    Test code:

    QMetaType metaType = QMetaType::fromName("Vector");
    Q_ASSERT(metaType.hasRegisteredDataStreamOperators()); // PASSES
    
    Vector vector;
    vector.mFoo = "bar";
    QVariant variant = QVariant::fromValue<Vector>(vector);
    
    QByteArray bytes;
    QDataStream stream(&bytes, QIODevice::WriteOnly);
    stream << variant;
    
    QDataStream stream2(&bytes, QIODevice::ReadOnly);
    QVariant variant2;
    stream >> variant2;
    Vector vector2 = variant2.value<Vector>();
    Q_ASSERT(vector2.mFoo == "bar"); // FAILS
    

    The vector is not restored correctly--in fact the variant is null when inspecting it. The stream read function is not even called.

    Is there something I'm missing? I assumed this is how serialization with custom types were supposed to work.

    Christian EhrlicherC 2 Replies Last reply
    0
    • P Per Ivar Bruheim

      I have implemented a custom QVariant type by registering the class with Q_DECLARE_METATYPE(Vector) and defining the stream operators.

      Vector.h:

      class Vector
      {
      public:
         Vector();
         QString mFoo;
      };
      
      Q_DECLARE_METATYPE(Vector)
      QDataStream& operator<<(QDataStream& stream, const Vector& v);
      QDataStream& operator>>(QDataStream& stream, Vector& v);
      

      Vector.cpp:

      #include "Vector.h"
      
      Vector::Vector()
      { }
      
      QDataStream& operator<<(QDataStream& stream, const GVector& v)
      {
         stream << v.mFoo;
         return stream;
      }
      
      QDataStream& operator>>(QDataStream& stream, Vector& v)
      {
         stream >> v.mFoo;
         return stream;
      }
      

      Test code:

      QMetaType metaType = QMetaType::fromName("Vector");
      Q_ASSERT(metaType.hasRegisteredDataStreamOperators()); // PASSES
      
      Vector vector;
      vector.mFoo = "bar";
      QVariant variant = QVariant::fromValue<Vector>(vector);
      
      QByteArray bytes;
      QDataStream stream(&bytes, QIODevice::WriteOnly);
      stream << variant;
      
      QDataStream stream2(&bytes, QIODevice::ReadOnly);
      QVariant variant2;
      stream >> variant2;
      Vector vector2 = variant2.value<Vector>();
      Q_ASSERT(vector2.mFoo == "bar"); // FAILS
      

      The vector is not restored correctly--in fact the variant is null when inspecting it. The stream read function is not even called.

      Is there something I'm missing? I assumed this is how serialization with custom types were supposed to work.

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

      You forgot qRegisterMetaTypeStreamOperators<>() : https://forum.qt.io/topic/1690/qvariant-save-load-unable-to-save-type-x/4

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

      P 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        You forgot qRegisterMetaTypeStreamOperators<>() : https://forum.qt.io/topic/1690/qvariant-save-load-unable-to-save-type-x/4

        P Offline
        P Offline
        Per Ivar Bruheim
        wrote on last edited by
        #3

        @Christian-Ehrlicher I'm using Qt 6, where that function has been removed. According to the docs the stream operators are detected automatically.

        Christian EhrlicherC 1 Reply Last reply
        0
        • P Per Ivar Bruheim

          @Christian-Ehrlicher I'm using Qt 6, where that function has been removed. According to the docs the stream operators are detected automatically.

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

          The you should at least call qRegisterMetaType<>().

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

          P 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            The you should at least call qRegisterMetaType<>().

            P Offline
            P Offline
            Per Ivar Bruheim
            wrote on last edited by
            #5

            @Christian-Ehrlicher I've tried that too, but it still doesn't work.

            Christian EhrlicherC 1 Reply Last reply
            0
            • P Per Ivar Bruheim

              @Christian-Ehrlicher I've tried that too, but it still doesn't work.

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

              I tried your code here (no copy and paste) with Qt6. 6 and it worked correct, even without qRegisterMetaType<>(). Will have to find out the difference.

              /edit: please output the bytearray with qDebug to see if it was properly serialized.

              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
              • P Per Ivar Bruheim

                I have implemented a custom QVariant type by registering the class with Q_DECLARE_METATYPE(Vector) and defining the stream operators.

                Vector.h:

                class Vector
                {
                public:
                   Vector();
                   QString mFoo;
                };
                
                Q_DECLARE_METATYPE(Vector)
                QDataStream& operator<<(QDataStream& stream, const Vector& v);
                QDataStream& operator>>(QDataStream& stream, Vector& v);
                

                Vector.cpp:

                #include "Vector.h"
                
                Vector::Vector()
                { }
                
                QDataStream& operator<<(QDataStream& stream, const GVector& v)
                {
                   stream << v.mFoo;
                   return stream;
                }
                
                QDataStream& operator>>(QDataStream& stream, Vector& v)
                {
                   stream >> v.mFoo;
                   return stream;
                }
                

                Test code:

                QMetaType metaType = QMetaType::fromName("Vector");
                Q_ASSERT(metaType.hasRegisteredDataStreamOperators()); // PASSES
                
                Vector vector;
                vector.mFoo = "bar";
                QVariant variant = QVariant::fromValue<Vector>(vector);
                
                QByteArray bytes;
                QDataStream stream(&bytes, QIODevice::WriteOnly);
                stream << variant;
                
                QDataStream stream2(&bytes, QIODevice::ReadOnly);
                QVariant variant2;
                stream >> variant2;
                Vector vector2 = variant2.value<Vector>();
                Q_ASSERT(vector2.mFoo == "bar"); // FAILS
                

                The vector is not restored correctly--in fact the variant is null when inspecting it. The stream read function is not even called.

                Is there something I'm missing? I assumed this is how serialization with custom types were supposed to work.

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

                @Per-Ivar-Bruheim said in Simple custom QVariant serialization not working:

                QVariant variant2;
                stream >> variant2;

                OK, you should correct this.

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

                P 2 Replies Last reply
                1
                • Christian EhrlicherC Christian Ehrlicher

                  @Per-Ivar-Bruheim said in Simple custom QVariant serialization not working:

                  QVariant variant2;
                  stream >> variant2;

                  OK, you should correct this.

                  P Offline
                  P Offline
                  Per Ivar Bruheim
                  wrote on last edited by
                  #8

                  @Christian-Ehrlicher @Christian-Ehrlicher Thank you. Such an embarrasingly simple mistake.

                  1 Reply Last reply
                  1
                  • P Per Ivar Bruheim has marked this topic as solved on
                  • Christian EhrlicherC Christian Ehrlicher

                    @Per-Ivar-Bruheim said in Simple custom QVariant serialization not working:

                    QVariant variant2;
                    stream >> variant2;

                    OK, you should correct this.

                    P Offline
                    P Offline
                    Per Ivar Bruheim
                    wrote on last edited by
                    #9
                    This post is deleted!
                    1 Reply Last reply
                    0

                    • Login

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