Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QQmlPropertyMap as return type
Qt 6.11 is out! See what's new in the release blog

QQmlPropertyMap as return type

Scheduled Pinned Locked Moved Solved QML and Qt Quick
10 Posts 4 Posters 1.7k 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.
  • T Offline
    T Offline
    topse
    wrote on last edited by topse
    #1

    Hi,
    I want wo exchange dynamic data between C++ and QML (again...). My idea is: I create a C++-Class derived from QQmlPropertyMap. Then in C++ I create lists from that kind of object and I register an QObject to QML, which has a function that can return those objects:

    C++:

      broker = new DataBroker(...);
      engine->rootContext()->setContextProperty("broker", broker);
    

    QML:

          // broker.loadQml return a QQmlPropertyMap *
          // does not matter if casted to QObject*
          // it does not work to access or modify properties
          data = broker.loadQml("1234");
    

    When using QQmlPropertyMap as return type of the function, it even gives an error: "Unknown method return type: QQmlPropertyMap*"
    When usign QObject* as return type (that means cast the QQmlPropertyMap* to QObject*) it does not give an error, but properties are not accessible to QML.

    Can anyone give a hint?

    Thanks and best regards,
    Tobias

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
      wrote on last edited by dheerendra
      #2

      May be you missed registering the meta type as QQmlPropertyMap is not a standard type.

      C++ code
      
      qRegisterMetaType<QQmlPropertyMap*>("QQmlPropertyMap");
      
      QQmlPropertyMap* MyAuth::getValues()
      {
          QQmlPropertyMap *ownerData = new QQmlPropertyMap;
          ownerData->insert("name", QVariant(QString("Dheerendra")));
          ownerData->insert("email", QVariant(QString("dheerendra@pthinks.com")));
          return ownerData;
      }
      
      QML Code
      
                        var obj = login.getValues();
                        console.log("**** =",obj.name)
                        console.log("**** =",obj.email)
      

      Dheerendra
      @Community Service
      Certified Qt Specialist
      https://www.pthinks.com

      1 Reply Last reply
      2
      • T Offline
        T Offline
        topse
        wrote on last edited by topse
        #3

        @dheerendra said in QQmlPropertyMap as return type:

        qRegisterMetaType<QQmlPropertyMap*>("QQmlPropertyMap");

        Jepp... when registering the Meta-Type then at least the error goes away, but the properties are still inaccessible :-/

        Best Regards,
        Tobias

        1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
          wrote on last edited by
          #4

          Please do the code as suggested by me. Otherwise you can share complete code snippet. We will help you.

          Dheerendra
          @Community Service
          Certified Qt Specialist
          https://www.pthinks.com

          1 Reply Last reply
          1
          • T Offline
            T Offline
            topse
            wrote on last edited by topse
            #5

            I did.

            My feeling is, the problem is in qv4objectwrapper.cpp, in function CallArgument::initAsType. I think, using QQmlPropertyMap is only valid if set as context property (and not as return type of a function)?

            Best Regards,
            Tobias

            1 Reply Last reply
            0
            • GrecKoG Offline
              GrecKoG Offline
              GrecKo
              Qt Champions 2018
              wrote on last edited by
              #6

              Can you share a reproducible minimal example?

              I have no problems using QQmlPropertyMap* as a return type in my projects.
              Make sure to qmlRegisterType QQmlPropertyMap, not QQmlPropertyMap*.

              T 1 Reply Last reply
              0
              • GrecKoG GrecKo

                Can you share a reproducible minimal example?

                I have no problems using QQmlPropertyMap* as a return type in my projects.
                Make sure to qmlRegisterType QQmlPropertyMap, not QQmlPropertyMap*.

                T Offline
                T Offline
                topse
                wrote on last edited by topse
                #7

                main:

                int main(int argc, char *argv[])
                {
                  qRegisterMetaType<QQmlPropertyMap*>("QQmlPropertyMap");
                  qmlRegisterType<QQmlPropertyMap>();
                  TestQmlJsonConnect::exec(argc, argv);
                
                  return 0;
                }
                
                void TestQmlJsonConnect::exec(int argc, char *argv[])
                {
                  broker = new DataBroker(provider, "JsonData");
                  Setup setup;
                  quick_test_main_with_setup(argc, argv, "TestQmlJsonConnect", ":/qml", &setup);
                  delete broker;
                  delete provider;
                }
                void Setup::qmlEngineAvailable(QQmlEngine *engine)
                {
                  engine->rootContext()->setContextProperty("broker", broker);
                }
                
                QQmlPropertyMap *DataBroker::loadQml(const QUuid &uuid)
                {
                  QQmlPropertyMap *p = new QQmlPropertyMap();
                  p->insert("info", "test");
                  return p;
                }
                
                import QtQuick 2.12
                import QtTest 1.12
                
                TestCase {
                    name: "tst_QmlJsonConnect.qml"
                
                    function test_one() {
                      data = broker.loadQml("1234");
                      console.log(data);              // qml: [object Object]
                      console.log(data.info);         // qml: undefined
                      compare(data.info, "test");     // compare fails -> test ends
                      data.info = "Hallo";
                      compare(data.info, "Hallo");
                    }
                }
                

                :-/

                1 Reply Last reply
                0
                • IntruderExcluderI Offline
                  IntruderExcluderI Offline
                  IntruderExcluder
                  wrote on last edited by
                  #8

                  Cannot build your example rn, but have you ever tried to print 'data' keys? Like console.log(Object.keys(data)).

                  T 1 Reply Last reply
                  0
                  • IntruderExcluderI IntruderExcluder

                    Cannot build your example rn, but have you ever tried to print 'data' keys? Like console.log(Object.keys(data)).

                    T Offline
                    T Offline
                    topse
                    wrote on last edited by
                    #9

                    @IntruderExcluder

                    You are right... I did a really small Application (about 3 lines of code :-)) and there it works. I am going to check, what is wrong with my real "Application" -> This is currently only a Testapplication (using testlib and testcases). Maybe the environment is different there...

                    Thanks for help,
                    Tobias

                    T 1 Reply Last reply
                    0
                    • T topse

                      @IntruderExcluder

                      You are right... I did a really small Application (about 3 lines of code :-)) and there it works. I am going to check, what is wrong with my real "Application" -> This is currently only a Testapplication (using testlib and testcases). Maybe the environment is different there...

                      Thanks for help,
                      Tobias

                      T Offline
                      T Offline
                      topse
                      wrote on last edited by
                      #10

                      @topse

                      Found the bug:

                      var data = broker.loadQml(...);
                      

                      Forget the var :-(

                      Thanks for help - it was very appreciated to read, that in principle using QQmlPropertyMap as return type works.

                      Best Regards,
                      Tobias

                      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