-
Suggestion
-
Resolution: Fixed
-
P2: Important
-
None
-
5789ae5 (dev), 85987e8 (dev), fdead8a (6.6), b926dfc (6.5), 2a4679a (6.5), 06b20fd (6.6)
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++?
| For Gerrit Dashboard: QTBUG-110821 | ||||||
|---|---|---|---|---|---|---|
| # | Subject | Branch | Project | Status | CR | V |
| 497667,2 | Doc: Explain how DEPENDENCIES helps with type resolution | dev | qt/qtdeclarative | Status: MERGED | +2 | 0 |
| 497907,2 | Doc: List QtQml value types separate from QtQuick value types | dev | qt/qtdeclarative | Status: MERGED | +2 | 0 |
| 498125,2 | Doc: List QtQml value types separate from QtQuick value types | 6.6 | qt/qtdeclarative | Status: MERGED | +2 | 0 |
| 498126,2 | Doc: List QtQml value types separate from QtQuick value types | 6.5 | qt/qtdeclarative | Status: MERGED | +2 | 0 |
| 498127,2 | Doc: Explain how DEPENDENCIES helps with type resolution | 6.5 | qt/qtdeclarative | Status: MERGED | +2 | 0 |
| 498128,2 | Doc: Explain how DEPENDENCIES helps with type resolution | 6.6 | qt/qtdeclarative | Status: MERGED | +2 | 0 |