2014-07-08 5 views
9

QT 및 QML을 완전히 새롭게 함. 나는이 때에 프로퍼티가 callValuehandRaiseXBB 두 배 사이의 관계를 기반으로 사각형의 색상을 설정하기 위해 노력하고있어,하지만 난 오류를If QML의 진술

예기치 않은 토큰을 얻을

"만약

은 규정 된 이름 아이디

예상

사람이 어떻게 도이 누구인지 말해 줄 수 틀렸어?

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 
     } 
    } 
} 
+4

그냥 QML은 선언적 언어이며, 당신이 기능에 필수적 코드를 작성할 수 있다는 것을 기억하거나 속성 바인딩은 포함하지만 객체 정의에는 포함되지 않습니다. – QtRoS

답변

14

당신은 이런 식으로 작업을 수행 할 수 있습니다

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

소원 내가 두 번 upvote 수 있습니다! 감사! –

+2

@ Q-bertsuit : qml은 자바 스크립트를 기반으로 작성되었으므로 배울 필요가 있습니다. – lpapp

1
:

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

또한 그것을 계산하고 함수의 반환 값과 색상 속성을 지정하는 함수를 만들 수

이것에 대한 또 다른 형태는

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

하지만 삼항 연산자를 사용하는 형식이 더 좋은 형식입니다!

QML은 자바 스크립트, 그럼 내가이 모든 itens 자바 스크립트 객체라는 것을 보라 "기반"방법에 :

var Rectangle: { 
    color: "red", 
    id: "id", 
    //then we can do this 
    setColor: function(_color) {this.color = _color} 
}