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

qmltc: QML Engine should instantiate qmltc-generated C++ class where available

    XMLWordPrintable

Details

    Description

      In the attached project, MyType.qml is processed by qmltc.

       

      Code

      // Main.qml
      import QtQuick
      import QmltcStudy
      
      Window {
          width: 800
          height: 600
          visible: true
      
          MyType {
              id: qmlObj
              objectName: "qmlObj"
          }
          Component.onCompleted: console.log("Created from QML:", qmlObj)
      }
      
      // main.cpp
      #include <QGuiApplication>
      #include <QQmlApplicationEngine>
      #include "mytype.h"
      
      int main(int argc, char *argv[])
      {
          QGuiApplication app(argc, argv);
      
          QQmlApplicationEngine engine;
          engine.loadFromModule("QmltcStudy", "Main");
      
          QmltcStudy::MyType cppObj(&engine);
          qDebug() << "Created from C++:" << &cppObj;
      
          auto qmlObj = engine.rootObjects().first()->findChild<QObject*>("qmlObj");
          qDebug() << "Attempting to cast QML to C++:" << qobject_cast<QmltcStudy::MyType*>(qmlObj);
      
          return app.exec();
      }
      

       

      Debug output

      qml: Created from QML: MyType_QMLTYPE_1(0x2ca59d22340, "qmlObj")
      Created from C++: QmltcStudy::MyType(0xe67e3ff968)
      Attempting to cast QML to C++: QObject(0x0)
      

       

      Analysis
      There are 2 different types with 2 different QMetaObjects.

      I was under the impression that the QML engine would instantiate the qmltc-generated C++ class instead of using MyType.qml, but it looks like this isn't the case. I'm not sure if this is a bug or unimplemented functionality.

      Attachments

        Issue Links

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

          Activity

            People

              qtqmlteam Qt Qml Team User
              skoh-qt Sze Howe Koh
              Votes:
              1 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:

                Gerrit Reviews

                  There are no open Gerrit changes