If condition on a color property
-
Hello,
i've searched for this both on google and in this forum, but can't seem to find any answer.
I'm learning PySide and QtQuick while I contribute with some tutorials for the PySide wiki documentation, and while doing a simple example (http://developer.qt.nokia.com/wiki/PySide_and_QML_Playground ) i've come up with something which i consider not very intuitive. The example is this:@
import Qt 4.7Rectangle {
width: 200
height: 200
color: "white"Text { text: "Hello World" anchors.centerIn: parent font.pixelSize: 24 color: 'black' MouseArea { anchors.fill: parent onClicked: { console.log("Mouse clicked!", parent.color) if (parent.color == "#000000") parent.color = 'blue'; else parent.color = 'black'; } } }
}
@So, this shows a text in black which clicked, toggles to blue and vice-versa, and it's working. What i consider not intuitive is the code on line 18, namely @if (parent.color == "#000000")@
As i used @color: 'black'@ in the Text property, the intuitive step is to test the color property like @if (parent.color == 'black')@ but it doesn't work. I think it is doing something like comparing the color with a string.Not very important, but took me some time to circumvent.. Does anyone know if this is an error or a design issue?
Thanks,
João Ventura -
hi. nice. I think it's an error. I think the comparison is between strings and not colors. You should report it on http://bugreports.qt.nokia.com
-
[quote author="mbrasser" date="1295576948"]Hi,
Javascript doesn't have any notion of a "color object", so in the end this comes down to a string comparison ('==' in JS will try to convert to a common type to do the comparison).
[/quote]Hi, thats what i thought it was.. But i think the intuitive step is to compare using 'black' and not '#000000'. If a framework starts to have many little details which are non intuitive, it needs to have bigger and complicated documentation just to explain small things..
[quote author="2beers" date="1295573143"]
hi. nice. I think it's an error. I think the comparison is between strings and not colors. You should report it on http://bugreports.qt.nokia.com
[/quote]From the words of mbrasser, it seems that is a property of the javascript usage, and not an error or even a lack on the design. But if it is an error, i can try to report it (how and where?).
Btw, Qt Quick seems a very good framework. Had they used Python instead of Javascript and it would be excelent! But i understand the target audience of Qt Quick. Maybe someday, someone makes it work with other scripting languages inside..
Thanks,
João Ventura -
-
Hi Volker,
[quote author="Volker" date="1295601142"]
I'm not a QML expert, I would try something like this:@
if(parent.color == QColor("black"))
@
[/quote]i've tried before, but got the following in the console:
@
Mouse clicked! #000000
file:///home/jventura/workspace/QtTests/QML/view.qml:19: ReferenceError: Can't find variable: QColor
@I think JavaScript also doesn't know about internal Qt types (at least in this way without "registering" or that kind of things which I still didn't explore yet)..
I also tried things like color.black, etc., but i think only things like '#000000' work..But no problem, this is only a simple thing and can be circunvented. With was just to make the point that sometimes the users find little details that the framework developers didn't thought it would be used that way..
Thank you all,
João Ventura -
Seems that does not work at the moment - see "QTBUG-14731":http://bugreports.qt.nokia.com/browse/QTBUG-14731
-
[quote author="Volker" date="1295611584"]Seems that does not work at the moment - see "QTBUG-14731":http://bugreports.qt.nokia.com/browse/QTBUG-14731[/quote]
Hi Volker, seems the answer to your suggestion, but not the answer to the original supposition, although it may serve. As i said, i don't know if the @if (parent.color == 'black')@ not working as intuitive is an error or a language design decison, so i will leave that for the experts..
Thanks for your feedback,
João Ventura -
The color 'black' is just an alias for '#000000', so internally it is converted to the latter. That's why your comparison with 'black' fails. Unfortunately there seems to be no way to convert 'black' to '#000000' on the fly to enable the proper comparison.
-
I had the same problem. I've tried many, many different approaches, but the only one that works is this:
if (parent.color != Qt.rgba(0, 0, 0, 1)) parent.color = "black"; else parent.color = "blue";
For some bizarre reason, these always evaluate to false:
if (parent.color == Qt.rgba(0, 0, 0, 1))
if (parent.color === Qt.rgba(0, 0, 0, 1))It's the same exact comparison, but for equality.
-
Sorry about the formatting; I'm having trouble with colors and indentation...
-
This looks a little better. Still can't get indent to work, though...
I had the same problem. I’ve tried many, many different approaches, but the only one that works is this:
[color=red]if[/color] (parent.color != [color=blue]Qt[/color].rgba(0, 0, 0, 1)) parent.color = “black”;
[color=red]else[/color] parent.color = “blue”;For some bizarre reason, these always evaluate to false:
[color=red]if[/color] (parent.color [color=blue]Qt[/color].rgba(0, 0, 0, 1))
[color=red]if[/color] (parent.color = [color=blue]Qt[/color].rgba(0, 0, 0, 1))It’s the same exact comparison, but for equality.
-
When I posted the above, some characters were removed.
There should be a double and then a triple equal sign between compared colors like so:
[color=red]if[/color] (parent.color == [color=blue]Qt[/color].rgba(0, 0, 0, 1))
[color=red]if[/color] (parent.color === [color=blue]Qt[/color].rgba(0, 0, 0, 1))
-
I did more experimenting on this. It turns out that white is the only color this comparison works on. Both of these work fine:
[color=red]if[/color](parent.color != [color=blue]Qt[/color].rgba(255, 255, 255, 1) )
[color=red]if[/color](parent.color == [color=blue]Qt[/color].rgba(255, 255, 255, 1) )
But any other color either evaluates false for every comparison or true for every comparison, depending on if the test is for equality or inequality. I messed with opacity values - didn't help.
Weird.
-
I'm new to QT development and have started playing around with some simple programs using latest QT 5.7.
This issue is still seen in my latest setup.
Snapshot of the QT Setup Used from the "About Panel"
Qt Creator 4.0.3
Based on Qt 5.7.0 (Clang 7.0 (Apple), 64 bit)
Built on Jul 5 2016 01:07:01 -
@SJ_Arjun
use the colorEqual method