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

No good way to check if color is valid

    XMLWordPrintable

Details

    • e80ef5934e71bcc52bdcabb6d3fe1d1b100d75c4

    Description

      Problem

      Using QColor::isValid() it is easy to check if a color has been assigned a valid color value or not. On QML side, there seems to be no easy way to do the same. While QML does distinguish between invalid and valid colors (see option3 and option4) checking and even resetting color properties to their initial (invalid) state, seems not possible in the way it would be expected.

      Example Code

      import QtQuick 2.9
      QtObject{
      property color color
      // readonly property color invalidColor: ""
      readonly property color invalidColor: { return "" }
      Component.onCompleted: {
        console.log ("Checking if " + color + " is invalid.");
        if (color === "undefined")
          console.log ("option1");
        if (color === "")
          console.log ("option2");
        if (color === invalidColor)
          console.log ("option3");
        if (!Qt.colorEqual(Qt.rgba(color.r, color.g, color.b, color.a), color))
          console.log ("option4");
      }}
      

      qmlscene output is:

      qml: Checking if #000000 is invalid.
      qml: option3
      qml: option4

      Workaround - Option 3:

      Declare a readonly color property that is not assigned a value. This can then be used for comparison. Note that assigning "" directly in the property definition produces an "Invalid property assignment: color expected" error.

      Workaround - Option 4:

      Reconstruct color using Qt.rgba and check if the reconstructed color still compares to the original using Qt.colorEqual

      Attachments

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

        Activity

          People

            ulherman Ulf Hermann
            dkdai dkdai
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes