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. If condition on a color property
Qt 6.11 is out! See what's new in the release blog

If condition on a color property

Scheduled Pinned Locked Moved QML and Qt Quick
17 Posts 8 Posters 16.8k 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.
  • J Offline
    J Offline
    jventura
    wrote on last edited by
    #1

    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.7

    Rectangle {
    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

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

      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

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mbrasser
        wrote on last edited by
        #3

        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).

        Regards,
        Michael

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jventura
          wrote on last edited by
          #4

          [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

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #5

            All colors will eventually be converted to the internal number representation in QColor. If you compare two colors by name you should convert them accordingly.

            I'm not a QML expert, I would try something like this:

            @
            if(parent.color == QColor("black"))
            @

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0
            • J Offline
              J Offline
              jventura
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #7

                Seems that does not work at the moment - see "QTBUG-14731":http://bugreports.qt.nokia.com/browse/QTBUG-14731

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  jventura
                  wrote on last edited by
                  #8

                  [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

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    Kxyu
                    wrote on last edited by
                    #9

                    i guess it's a bug. well 'black' is certainly a string, but when you aply it to color property it is automaticli transformed into QColor. So if you compare a string with QColor it's also possible to convert it. just a little oversight I think

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      goetz
                      wrote on last edited by
                      #10

                      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.

                      http://www.catb.org/~esr/faqs/smart-questions.html

                      1 Reply Last reply
                      0
                      • R Offline
                        R Offline
                        rwtmoorehotmail.com
                        wrote on last edited by
                        #11

                        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.

                        1 Reply Last reply
                        0
                        • R Offline
                          R Offline
                          rwtmoorehotmail.com
                          wrote on last edited by
                          #12

                          Sorry about the formatting; I'm having trouble with colors and indentation...

                          1 Reply Last reply
                          0
                          • R Offline
                            R Offline
                            rwtmoorehotmail.com
                            wrote on last edited by
                            #13

                            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.

                            1 Reply Last reply
                            0
                            • R Offline
                              R Offline
                              rwtmoorehotmail.com
                              wrote on last edited by
                              #14

                              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))

                              1 Reply Last reply
                              0
                              • R Offline
                                R Offline
                                rwtmoorehotmail.com
                                wrote on last edited by
                                #15

                                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.

                                1 Reply Last reply
                                0
                                • S Offline
                                  S Offline
                                  SJ_Arjun
                                  wrote on last edited by
                                  #16

                                  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

                                  raven-worxR 1 Reply Last reply
                                  0
                                  • S SJ_Arjun

                                    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

                                    raven-worxR Offline
                                    raven-worxR Offline
                                    raven-worx
                                    Moderators
                                    wrote on last edited by
                                    #17

                                    @SJ_Arjun
                                    use the colorEqual method

                                    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                                    If you have a question please use the forum so others can benefit from the solution in the future

                                    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