如果在QML中声明

Q-b*_*uit 10 user-interface qt qml qt-quick

完全是QT和QML的新手.我正在尝试设置基于两个属性格式双打之间的关系的矩形的颜色callValuehandRaiseXBB,但我得到的错误

意外令牌如果"

期望一个合格的名称ID

谁能告诉我我做错了什么?

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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Ret*_*nja 21

你可以这样做:

color: (hand.callValue >= hand.handRaiseXBB) ? hand.handFoldColor : hand.handCallColor
Run Code Online (Sandbox Code Playgroud)

您还可以创建一个函数来计算它,然后使用函数的返回值赋值color属性:

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

  • 希望我能两次投票!谢谢! (2认同)
  • @ Q-bertsuit:qml是基于javascript构建的,所以看起来你需要学习它. (2认同)