QML中的if语句 [英] If statement in QML
本文介绍了QML中的if语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
全新的 QT 和 QML.我正在尝试根据两个属性双打 callValue
和 handRaiseXBB
之间的关系设置矩形的颜色,但出现错误
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
意外的令牌如果"
和
需要一个合格的名称 id
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
}
}
}
推荐答案
你可以这样做:
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()
这篇关于QML中的if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文