Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-119093

Qt6 QML can't access exposed c++ QObject property of QObject

    XMLWordPrintable

Details

    • Windows

    Description

       

      class ADV_Application : public QObject
      {
          Q_OBJECT
          Q_PROPERTY(ADV_Desktop* desktop READ Desktop CONSTANT)
          QML_ELEMENT
          QML_UNCREATABLE("")
      public:
          explicit ADV_Application();
          ~ADV_Application();    Q_INVOKABLE ADV_Desktop* getDesktop() const;   
            ADV_Desktop* Desktop() const { return myDesktop; }
          virtual void SetDesktop(ADV_Desktop* theDesktop);
      protected:
          ADV_Desktop* myDesktop;
      };
      
      class ADV_Desktop : public QObject
      {
          Q_OBJECT
          Q_PROPERTY(ADV_Application* applicaton READ Application CONSTANT)
          QML_ELEMENT
          QML_UNCREATABLE("")
      public:
          explicit ADV_Desktop();
          ~ADV_Desktop();   
            ADV_Application* Application() const {return myApplication;}
          virtual void BindApplication(ADV_Application* theApplication);
      protected:
          ADV_Application* myApplication;
      };
      
      int main(int argc, char *argv[])
      {
          QGuiApplication app(argc, argv);    
            QQmlApplicationEngine engine;
          const QUrl url(u"qrc:/Main.qml"_qs);
          QObject::connect(&engine, &QQmlApplicationEngine::objectCreationFailed,
              &app, []() { QCoreApplication::exit(-1); },
              Qt::QueuedConnection);    
            ADV_Application* myApplication = new ADV_Application();
          ADV_Desktop* myDesktop = new ADV_Desktop();
          myApplication->SetDesktop(myDesktop);   
            myApplication->setObjectName("MyApplication");
          myDesktop->setObjectName("MyDesktop");    
            engine.rootContext()->setContextProperty("myApplication", myApplication);
           engine.load(url);    
            return app.exec();
      }
       
      Window {
          width: 640
          height: 480
          visible: true
          title: qsTr("Hello World")    
      onVisibleChanged: {
              console.info(myApplication)
              console.info(myApplication.desktop) // bug here
              console.info(myApplication.objectName)
              console.info(myApplication.desktop.objectName) // bug here
          }
      }

       

      Two classes ADV_Application  and ADV_Desktop which are QObject classes. Both have a property to access each other. In the main function, myApplication is registered as context property for QML main.qml.

      But the result shows like below:

      qml: ADV_Application(0x1dec0e8e660, "MyApplication")
      qml: QVariant(ADV_Desktop, 0x1dec0e8e140)*
      qml: MyApplication
      qml: undefined

      myApplication.desktop debug result: qml: QVariant(ADV_Desktop, 0x1dec0e8e140)*

      myApplication.desktop.ObjectName is not valid and the debug result is qml: undefined.

       

       

       

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            qtqmlteam Qt Qml Team User
            bugman Vico Liang
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes