Details
-
Technical task
-
Resolution: Unresolved
-
P2: Important
-
None
Description
// Constants.js const width = 800 const height = 600 // main.qml import QtQuick import "Constants.js" as Constants Window { width: Constants.width height: Constants.height visible: true }
This works fine at runtime, but qmlsc produces compilation warnings:
Warning: main.qml:5:22: Property "width" not found on type "Constants" width: Constants.width ^^^^^ Warning: main.qml:5:22: Could not compile binding for width: Cannot load property width from QJSValue of Constants. width: Constants.width ^^^^^ Warning: main.qml:6:23: Property "height" not found on type "Constants" height: Constants.height ^^^^^^ Warning: main.qml:6:23: Could not compile binding for height: Cannot load property height from QJSValue of Constants. height: Constants.height ^^^^^^
Suggestion
Since const primitives are immutable, their values can be inlined as part of the QML compilation process (like C++ constexpr)
Workaround to eliminate warnings
Store constants in a singleton's readonly properties instead:
// Constants.qml pragma Singleton import QtQml QtObject { readonly property int width: 800 readonly property int height: 600 }