Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-76025 Specify the QML language and write a QML-to-C++ compiler
  3. QTBUG-106715

qmlsc: Add ability to inline const primitive values from *.js files

    XMLWordPrintable

Details

    • Technical task
    • Resolution: Unresolved
    • P2: Important
    • 6.x
    • QML: Compiler
    • 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
      }
      

      Attachments

        Activity

          People

            qtqmlteam Qt Qml Team User
            skoh-qt Sze Howe Koh
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated: