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

QML: Add a compilable way to convert color to string

XMLWordPrintable

      Currently, there is no way for qmlsc to convert a color to a string.

      The only way to comply with `pragma strict` is to pass the color value to C++, call QColor::name(), and then pass it back to QML.

      //pragma strict
      import QtQuick
      
      Window {
          id: root
          width: 640
          height: 480
          visible: true
          color: "grey"
      
          Column {
              id: tests
      
              // ===================
              // #1 -- #4 work fine and produce the same results at runtime
              // but they cannot be compiled by qmlsc
              // ===================
      
              // #1
              // Cannot assign binding of type QColor to QString [incompatible-type]
              Text { text: root.color }
      
      
              // #2
              // Cannot generate efficient code for call to property 'toString' of QVariant of
              // QQuickWindow::color with type QColor [compiler]
              Text { text: root.color.toString() }
      
      
              // #3
              // argument 0 contains QColor of QQuickWindow::color with type QColor but is expected
              // to contain the type QString [compiler]
              //         Text { text: Qt.atob(Qt.btoa(root.color)) }
              //                                 ^^^^
              Text { text: Qt.atob(Qt.btoa(root.color)) }
      
      
              // #4
              // Cannot generate efficient code for call to untyped JavaScript function [compiler]
              //         return Math.round(255*value).toString(16).padStart(2, '0')
              //                                      ^^^^^^^^
              function colorComp2Hex(value: double): string {
                  return Math.round(255*value).toString(16).padStart(2, '0')
              }
              Text {
                  text: {
                      const a = root.color.a >= 1 ? '' : tests.colorComp2Hex(root.color.a)
                      const r = tests.colorComp2Hex(root.color.r)
                      const g = tests.colorComp2Hex(root.color.g)
                      const b = tests.colorComp2Hex(root.color.b)
                      return `#${a}${r}${g}${b}`
                  }
              }
      
      
              // ===================
              // #5 -- #7 don't work at all
              // ===================
      
              // #5 (Attempt to addrress warning #2)
              // invalid cast from QColor of QQuickWindow::color with type QColor to QColor of
              // (component in Main.qml)::color with type QColor. You can only cast object types. [compiler]
              Text { text: (root.color as color).toString() }
      
      
              // #6 (Attempt to call QColor::name()
              // Member "name" not found on type "QColor" [missing-property]
              Text { text: root.color.name() }
      
      
              // #7 (Other attempt to call QColor::name()
              // Member "name" not found on type "QColor" [missing-property]
              Text { text: root.color.name }
          }
      }
      
      

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

            qt.team.quick.subscriptions Qt Quick and Widgets Team
            skoh-qt Sze Howe Koh
            Votes:
            2 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:

                There are no open Gerrit changes