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. Problem converting QVariantMap to QObject

Problem converting QVariantMap to QObject

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 778 Views 2 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.
  • M Offline
    M Offline
    MajidKamali
    wrote on last edited by
    #1

    Hi.
    I want to convert a QVariantMap to a QObject with corresponding properties.
    I tried both QJson and QtPropertySerializer. Both are great in converting simple data types (e.g. int), but can not convert to QList<int>
    I have a property of type QList<int> in my QObject-derived class and some other int properties.
    After calling QObjectHelper::qvariant2qobject or QtPropertySerializer::deserialize int properties are ok and have correct values, but QList<int> properties are all empty. Also there is no warnings or errors.
    What's the problem? how can I manually convert them to correct values?

    Thanks

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

      Hi
      Both QJson and QtPropertySerializer are external to Qt so unless other users here
      also used these 3rd party addon,
      its unlikely you can get an answer here and should maybe ask the authors about it.

      Also when you serialize with QtPropertySerializer to json, is the
      QList property even included in the output ?

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MajidKamali
        wrote on last edited by MajidKamali
        #3

        @mrjj Yes it is included in the qDebug output:
        ("savedCosts", QVariant(QList<int>, ))
        Although qDebug shows it empty. but when I deserialize this data back to another object, it has actual values :/

        Bart *b = new Bart;
        b->setSavedCosts({1, 2, 3}); // <- QList<int>
        qDebug() << QtPropertySerializer::serialize(b); // <- I showed part of the output, in above code
        Bart *b2 = new Bart;
        QtPropertySerializer::deserialize(b2, QtPropertySerializer::serialize(b));
        qDebug() << b2->savedCosts(); // prints (1, 2, 3)
        
        mrjjM 1 Reply Last reply
        0
        • M MajidKamali

          @mrjj Yes it is included in the qDebug output:
          ("savedCosts", QVariant(QList<int>, ))
          Although qDebug shows it empty. but when I deserialize this data back to another object, it has actual values :/

          Bart *b = new Bart;
          b->setSavedCosts({1, 2, 3}); // <- QList<int>
          qDebug() << QtPropertySerializer::serialize(b); // <- I showed part of the output, in above code
          Bart *b2 = new Bart;
          QtPropertySerializer::deserialize(b2, QtPropertySerializer::serialize(b));
          qDebug() << b2->savedCosts(); // prints (1, 2, 3)
          
          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @MajidKamali
          Ok, so QtPropertySerializer actually support it/knows Qlist and
          if it read it into b2 correctly then which part is not working for you?

          1 Reply Last reply
          0
          • M Offline
            M Offline
            MajidKamali
            wrote on last edited by MajidKamali
            #5

            @mrjj I changed the code a little bit and found something strange :|

            QObject *b = new Bart;
            b->setProperty("savedCosts", QVariant::fromValue(QList<int>{1, 2, 3}));
            qDebug() << QtPropertySerializer::serialize(b); // <- I showed part of the output, in above code
            QObject *b2 = new Bart;
            QtPropertySerializer::deserialize(b2, QtPropertySerializer::serialize(b));
            qDebug() << b2->property("savedCosts").toList(); // <- THIS LINE PRINTS EMPTY -> ()
            qDebug() << qobject_cast<Bart*>(b2)->savedCosts(); // <- BUT THIS DOES NOT -> (1, 2, 3)
            

            I thinks it's Qt-related now :D

            1 Reply Last reply
            0
            • M Offline
              M Offline
              MajidKamali
              wrote on last edited by
              #6

              This is property declaration in Bart Class

              Q_PROPERTY(QList<int> savedCosts READ savedCosts WRITE setSavedCosts)
              
              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