If statement in QML

Q-bertsuit picture Q-bertsuit · Jul 8, 2014 · Viewed 42.2k times · Source

Completely new to QT and QML. I'm trying to set the color of a rectangle based on the relationship between the two propery doubles callValue and handRaiseXBB, but I get the error

unexpected token if"

and

expected a qualified name id

Could anyone tell me what I am doing wrong?

import QtQuick 2.0

Item{
    id: hand

    property double callValue: 0.0

    property double handRaiseXBB: 100
    property string handCallColor: "green"
    property string handFoldColor: "grey"

    Rectangle {
        anchors.fill: hand
        if (hand.callValue >= hand.handRaiseXBB) {
            color: hand.handFoldColor
        }
        else {
            color: hand.handCallColor
        }
    }
}

Answer

Retired Ninja picture Retired Ninja · Jul 8, 2014

You can do it like this:

color: (hand.callValue >= hand.handRaiseXBB) ? hand.handFoldColor : hand.handCallColor

You could also make a function to calculate it and then assign the color property with the return value of the function:

function getHandColor()
{
    var handColor = hand.handCallColor
    if(hand.callValue >= hand.handRaiseXBB)
    {
        handColor = hand.handFoldColor
    }
    return handColor
}
color: getHandColor()