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

Improve documentation for finding required DEPENDENCIES for qt_add_qml_module()

    XMLWordPrintable

Details

    • 5789ae5 (dev), 85987e8 (dev), fdead8a (6.6), b926dfc (6.5), 2a4679a (6.5), 06b20fd (6.6)

    Description

      The code below produces the warning,

      Warning: main.qml:8:24: Could not compile binding for color: return type QColor cannot be resolved [compiler]
      

      To fix this warning, we need to add DEPENDENCIES QtQuick to our QML module, as explained at https://www.qt.io/blog/compiling-qml-to-c-making-types-visible This information should be part of the official documentation too.

       

      Code

      // mysingleton.h
      #include <QObject>
      #include <QQmlEngine>
      #include <QColor>
      
      class MySingleton : public QObject
      {
          Q_OBJECT
          QML_ELEMENT
          QML_SINGLETON
      
      public:
          MySingleton(QObject* parent = nullptr) : QObject(parent) {}
          Q_INVOKABLE QColor backgroundColor() const { return Qt::green; }
      };
      
      // main.qml
      import QtQuick
      import ColorCompilationStudy
      
      Window {
          width: 640
          height: 480
          visible: true
          color: MySingleton.backgroundColor()
      }
      
      // CMakeLists.txt
      cmake_minimum_required(VERSION 3.16)
      
      project(ColorCompilationStudy VERSION 0.1 LANGUAGES CXX)
      
      set(CMAKE_AUTOMOC ON)
      set(CMAKE_CXX_STANDARD_REQUIRED ON)
      
      find_package(Qt6 6.2 COMPONENTS Quick REQUIRED)
      
      qt_add_executable(appColorCompilationStudy
          main.cpp
      )
      
      qt_add_qml_module(appColorCompilationStudy
          URI ColorCompilationStudy
          VERSION 1.0
          AUTO_RESOURCE_PREFIX 
          QML_FILES main.qml
          SOURCES mysingleton.h
      #    DEPENDENCIES QtQuick
      
      ### How can I know that adding "QtQuick" as a DEPENDENCY lets qmlsc resolve QColor?
      )
      
      target_link_libraries(appColorCompilationStudy
          PRIVATE Qt6::Quick)
      

       

      Possible solution
      Perhaps at the top of the class reference, similar to the Header/CMake/qmake details required to use the class in C++?

      Attachments

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

        Activity

          People

            ulherman Ulf Hermann
            skoh-qt Sze Howe Koh
            Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: