Why can't I use ternary operator with onClicked



  • My code looks like this:

    Rectangle {
        id: background
        color: "steelblue"
    }
    Image {
        id: pic
        source: "pic.png"
    }
    
    MouseArea {
        id: mouseArea
        anchors.fill: pic
        onClicked: background.color = (background.color === "steelblue") ? "purple" : "steelblue"
    }
    

    The result is that background changes its color only once to "steelblue" and no again. I want it to change its color from 1 to 2 and vice versa every time I click on it. What I can do?



  • I tried even variations with js functions and etc. Nothing helped.


  • Lifetime Qt Champion

    Hi,

    There's a conversion done when you set the color property thus currently what you are doing is comparing a color with a string.

    The solution is: background.color = Qt.colorEqual(background.color, "steelblue") ? "purple" : "steel blue"


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.