Whats wrong with if else? [SOLVED]



  • Hi guys, trying to make a toggle (tick button) for making a vendor "favorite" in my demo.
    Issue is: the code below works good to change property "favorite" from 0 to 1, but I tried to implement logic if-else to ensure if the button is pressed again it could be turned off (changed from 1 to 0). What am I doing wring here?

    @MouseArea {
    id: region
    anchors.fill: parent
    onClicked: favIcon.toggle(), vendorsModel.setProperty(index, "favorite", 1? 1:0)
    }@

    EDIT: added @ tags by VCsala



  • Seems to me there is no decision. It's always 1 because that's what you specify: if 1, then 1 else 0.



  • Try something like this (I assume your vendorsModel is ListModel)
    @
    MouseArea {
    id: region
    anchors.fill: parent
    onClicked: {
    favIcon.toggle()
    vendorsModel.setProperty(index, "favorite", vendorsModel.get(index).favority ? 0:1)
    }
    }
    @



  • SOLVED:) thanks both!!!


Log in to reply
 

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