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. Why can't I use ternary operator with onClicked
QtWS25 Last Chance

Why can't I use ternary operator with onClicked

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qmlquick
3 Posts 2 Posters 5.9k Views
  • 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.
  • sosunS Offline
    sosunS Offline
    sosun
    wrote on last edited by sosun
    #1

    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?

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

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

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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"

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        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