What's wrong with this MouseArea logic
-
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?
-
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. -
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. -
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?
-
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. -
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.
-
qttester5:
Did you ever get your code to work?