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

qmlsc: Problems with context properties

    XMLWordPrintable

Details

    • Windows

    Description

      main.cpp:

          QQmlApplicationEngine engine;
          engine.rootContext()->setContextProperty("TestObject", new TestObject());
          const QUrl url(QStringLiteral("qrc:/main.qml"));
          QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                           &app, [url](QObject *obj, const QUrl &objUrl) {
              if (!obj && url == objUrl)
                  QCoreApplication::exit(-1);
          }, Qt::QueuedConnection);
          engine.load(url);
      

      Qml:

      import QtQuick 2.12
      import QtQuick.Window 2.12
      
      import QtQuick.Controls 2.3
      
      import com.test 1.0
      
      Window {
          width: 640
          height: 480
          visible: true
          title: qsTr("Hello World")
      
          Label {
              anchors.centerIn: parent
              text: "TestObject value: " + TestObject.value
          }
      }
      

      Works (shows "10" in Qml window)

      #include <QObject>
      #include <QQmlEngine>
      
      class TestObject : public QObject
      {
          Q_OBJECT
          Q_PROPERTY(int value READ get_value CONSTANT FINAL)
      public:
          explicit TestObject(QObject *parent = nullptr);
      
      public Q_SLOTS:
          int get_value() const {
              return 10;
          }
      };
      

      Compilation output:

      08:30:00: Running steps for project sttest...
      08:30:00: Starting: "C:\QtCommercial\Tools\CMake_64\bin\cmake.exe" --build C:/Users/dev/Desktop/Projects/test/build-sttest-Desktop_Qt_6_4_0_MSVC2019_64bit-Debug --target all
      [1/21 29.3/sec] Generating com/test/main.qml
      [2/21 51.5/sec] Generating .rcc/qmlcache/sttest_qmlcache_loader.cpp
      [3/21 27.5/sec] Running qmlimportscanner for sttest
      [4/21 10.8/sec] Automatic MOC and UIC for target sttest
      [5/21 12.5/sec] Running AUTOMOC file extraction for target sttest
      [6/21 14.7/sec] Running rcc for resource qmake_com_test
      [7/21 16.8/sec] Running rcc for resource sttest_raw_qml_0
      [8/21 18.5/sec] Automatic RCC for qml.qrc
      [9/21 20.0/sec] Running moc --collect-json for target sttest
      [10/21 18.8/sec] Automatic QML type registration for target sttest
      [11/21 16.5/sec] Generating .rcc/qmlcache/sttest_main_qml.cpp
      Warning: main.qml:4:1: Warnings occurred while importing module "QtQuick.Controls":
      import QtQuick.Controls 2.3
      ^^^^^^
      ---
      Warning: QtQuick.Controls uses optional imports which are not supported. Some types might not be found.
      ---
      
      Info: main.qml:4:1: Unused import at main.qml:4:1
      import QtQuick.Controls 2.3
      ^^^^^^
      Warning: main.qml:16:38: Unqualified access
              text: "TestObject value: " + TestObject.value
                                           ^^^^^^^^^^
      Error: main.qml:16:38: Could not compile binding for text: Cannot access value for name TestObject
              text: "TestObject value: " + TestObject.value
                                           ^^^^^^^^^^
      [12/21 13.2/sec] Building CXX object CMakeFiles\sttest.dir\.rcc\qrc_sttest_raw_qml_0.cpp.obj
      [13/21 14.3/sec] Building CXX object CMakeFiles\sttest.dir\.rcc\qrc_qmake_com_test.cpp.obj
      [14/21 15.4/sec] Building CXX object CMakeFiles\sttest.dir\sttest_autogen\EWIEGA46WW\qrc_qml.cpp.obj
      [15/21 7.1/sec] Building CXX object CMakeFiles\sttest.dir\sttest_qmltyperegistrations.cpp.obj
      [16/21 7.2/sec] Building CXX object CMakeFiles\sttest.dir\sttest_autogen\mocs_compilation.cpp.obj
      [17/21 7.7/sec] Building CXX object CMakeFiles\sttest.dir\testobject.cpp.obj
      [18/21 7.7/sec] Building CXX object CMakeFiles\sttest.dir\main.cpp.obj
      [19/21 7.9/sec] Building CXX object CMakeFiles\sttest.dir\.rcc\qmlcache\sttest_main_qml.cpp.obj
      [20/21 7.7/sec] Building CXX object CMakeFiles\sttest.dir\.rcc\qmlcache\sttest_qmlcache_loader.cpp.obj
      [21/21 7.7/sec] Linking CXX executable sttest.exe
      08:30:03: The process "C:\QtCommercial\Tools\CMake_64\bin\cmake.exe" exited normally.
      08:30:03: Elapsed time: 00:03.
      

      Does not work (shows "undefined" in Qml window):

      #include <QObject>
      #include <QQmlEngine>
      
      class TestObject : public QObject
      {
          Q_OBJECT
          QML_ELEMENT // This causes the "undefined" problem
          Q_PROPERTY(int value READ get_value CONSTANT FINAL)
      public:
          explicit TestObject(QObject *parent = nullptr);
      
      public Q_SLOTS:
          int get_value() const {
              return 10;
          }
      };
      

      Compilation output:

      08:28:18: Running steps for project sttest...
      08:28:18: Starting: "C:\QtCommercial\Tools\CMake_64\bin\cmake.exe" --build C:/Users/dev/Desktop/Projects/test/build-sttest-Desktop_Qt_6_4_0_MSVC2019_64bit-Debug --target all
      [1/21 31.1/sec] Generating com/test/main.qml
      [2/21 47.7/sec] Generating .rcc/qmlcache/sttest_qmlcache_loader.cpp
      [3/21 14.6/sec] Running qmlimportscanner for sttest
      [4/21 11.2/sec] Automatic MOC and UIC for target sttest
      [5/21 12.9/sec] Running AUTOMOC file extraction for target sttest
      [6/21 15.2/sec] Running rcc for resource sttest_raw_qml_0
      [7/21 17.7/sec] Running rcc for resource qmake_com_test
      [8/21 19.3/sec] Automatic RCC for qml.qrc
      [9/21 21.2/sec] Running moc --collect-json for target sttest
      [10/21 19.9/sec] Automatic QML type registration for target sttest
      [11/21 17.3/sec] Generating .rcc/qmlcache/sttest_main_qml.cpp
      Warning: main.qml:4:1: Warnings occurred while importing module "QtQuick.Controls":
      import QtQuick.Controls 2.3
      ^^^^^^
      ---
      Warning: QtQuick.Controls uses optional imports which are not supported. Some types might not be found.
      ---
      
      Info: main.qml:4:1: Unused import at main.qml:4:1
      import QtQuick.Controls 2.3
      ^^^^^^
      Warning: main.qml:16:49: Property "value" not found on type "TestObject"
              text: "TestObject value: " + TestObject.value
                                                      ^^^^^
      Error: main.qml:16:49: Could not compile binding for text: Cannot load property value from const QMetaObject of const QMetaObject.
              text: "TestObject value: " + TestObject.value
                                                      ^^^^^
      [12/21 13.9/sec] Building CXX object CMakeFiles\sttest.dir\.rcc\qrc_sttest_raw_qml_0.cpp.obj
      [13/21 15.0/sec] Building CXX object CMakeFiles\sttest.dir\.rcc\qrc_qmake_com_test.cpp.obj
      [14/21 16.2/sec] Building CXX object CMakeFiles\sttest.dir\sttest_autogen\EWIEGA46WW\qrc_qml.cpp.obj
      [15/21 6.9/sec] Building CXX object CMakeFiles\sttest.dir\testobject.cpp.obj
      [16/21 7.3/sec] Building CXX object CMakeFiles\sttest.dir\sttest_autogen\mocs_compilation.cpp.obj
      [17/21 7.6/sec] Building CXX object CMakeFiles\sttest.dir\sttest_qmltyperegistrations.cpp.obj
      [18/21 7.6/sec] Building CXX object CMakeFiles\sttest.dir\main.cpp.obj
      [19/21 7.9/sec] Building CXX object CMakeFiles\sttest.dir\.rcc\qmlcache\sttest_main_qml.cpp.obj
      [20/21 7.8/sec] Building CXX object CMakeFiles\sttest.dir\.rcc\qmlcache\sttest_qmlcache_loader.cpp.obj
      [21/21 7.8/sec] Linking CXX executable sttest.exe
      08:28:21: The process "C:\QtCommercial\Tools\CMake_64\bin\cmake.exe" exited normally.
      08:28:21: Elapsed time: 00:03.
      

      In summary there are 3 problems:

      1. QML_ELEMENT makes the context property inaccessible.
      2. Even without the QML_ELEMENT macro qmlint shows an error: "Error: main.qml:16:38: Could not compile binding for text: Cannot access value for name TestObject"
      3. Despite using the hints shown in the blog entry (https://www.qt.io/blog/compiling-qml-to-c-qtquick-controls-and-optional-imports) the warning "Warning: QtQuick.Controls uses optional imports which are not supported. Some types might not be found." is still shown.

      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
            Harald Meyer Harald Meyer
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes