Details
-
Suggestion
-
Resolution: Unresolved
-
P2: Important
-
None
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++?