Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. What's wrong with this MouseArea logic
Forum Updated to NodeBB v4.3 + New Features

What's wrong with this MouseArea logic

Scheduled Pinned Locked Moved QML and Qt Quick
7 Posts 4 Posters 2.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    qttester5
    wrote on last edited by
    #1

    New to Quick, doing some playing around:

    @ MouseArea {
    anchors.fill: parent
    onClicked: {
    if(parent.color=="blue") parent.color="green";
    else parent.color="blue";
    }
    }@

    When clicking the first time, color changes to blue as expected (it is not blue initially). But then, it does nothing. Since it is now blue, I'd expect it to then become green on next click. What am I missing?

    1 Reply Last reply
    0
    • A Offline
      A Offline
      AllenB2012
      wrote on last edited by
      #2

      I think tha I would retry this with the javascript syntax shown here
      http://www.w3schools.com/js/js_if_else.asp
      Basically, add the braces. May not be the problem, but I think that it's worth a try.

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        My guess is the string representation of parent.color isn't "blue", causing your condition to always fail, even once you've set it to "blue". Try using the HTML color code for blue ("#0000ff") instead.

        Edit:
        As you mentioned, setting the value to "blue" works just fine (as would "green"), but printing the value after you set it should result in the HTML code, I believe.

        1 Reply Last reply
        0
        • Q Offline
          Q Offline
          qttester5
          wrote on last edited by
          #4

          Hi Adam, even after setting parent.color to "red" and seeing red, trying to show parent.color via a Text element results in nothing, as if the string was empty. Does anyone know how the color element is actually stored/referenced?

          1 Reply Last reply
          0
          • C Offline
            C Offline
            chrisadams
            wrote on last edited by
            #5

            This is due to the comparison semantics of color properties. You can assign a string to a color (and it will do some clever conversions) but you cannot compare a string with a color and expect a meaningful result.

            Internally, a color property is stored as a QColor. Please see the documentation on Qt.colorEqual() at http://qt-project.org/doc/qt-5.0/qtqml/qml-qt.html#colorEqual-method or use Qt.rgba() as the RHS in the comparison http://qt-project.org/doc/qt-5.0/qtqml/qml-qt.html#rgba-method to ensure correctness.

            Cheers,
            Chris.

            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

              I found that if I used the following code, I encountered this error message: "Error: Unable to assign QColor to QString."

              @
              Rectangle
              {
              anchors.fill: parent
              color: "red"

              Text
              {
                  anchors.centerIn: parent
                  text: parent.color
              }
              

              }
              @

              If I modify the text property's assignment statement as follows, it displays the HTML color code, presumably because it implicitly calls QColor::name() when appending a QString.

              @
              text: parent.color + ""
              @

              Just bear in mind I only did so to see the string representation of the color value.

              1 Reply Last reply
              0
              • ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #7

                qttester5:

                Did you ever get your code to work?

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved