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