2014-07-25 6 views
1

QML에 off, pressed 및 on의 3 가지 상태가있는 버튼이 필요합니다. 지금은 코드에 대해 다음과 같은 내용이 있지만 작동하지 않습니다. 지금은 이전 상태를 기반으로 새 상태를 설정하는 토글 함수가 있지만 이전 상태가 "눌려진"경우 버튼을 토글하기 전에 상태를 어떻게 감지 할 수 있습니까? 다음QML 버튼 토글 전환

는 전체 클래스, 단지 관련 기능과 마우스 영역이 사용하는

Rectangle { 
     id: button1 
     function toggle(){ 
      var tempstate = button1.state 
      if (tempstate=="on") {tempstate = "off"; } else { tempstate ="on"} 
     } 

     MouseArea { 
      anchors.fill: parent 
      onPressed: parent.state = "pressed" 
      onReleased: parent.toggle() 
     } 

답변

1

3 가지 조건문을 사용하여이를 해결했습니다. 3 가지 상태를 사용하는 대신 2 가지 상태를 사용합니다. 여기서 각 상태는 MouseArea.pressed 속성을 기반으로하는 조건부를가집니다.

QML이 다소 이상하거나 부적절한 경우 좀더 관용적 인 해결책으로 저를 수정하십시오.

사각형의 color 속성을 이미지 항목의 소스 또는 다른 "3 가지 상태"클릭 가능 항목을 만드는 다른 속성으로 바꿀 수 있습니다.

Here is the link to QML's documentation 나는 이것이 실현 될 수 있음을 깨달았다.

예제 코드 :

Rectangle { 
    id: button1 
    state: "off" 
    color: "red" 
    MouseArea { 
     id: button1area 
     anchors.fill: parent 
     onReleased: parent.state == "on" ? parent.state = "off" : parent.state = "on" 
    } 
    states: [ 
     State { 
      name: "off" 
      PropertyChanges { target: button1; color: button1area.pressed ? "red" : "blue" } 
     }, 
     State { 
      name: "on" 
      PropertyChanges { target: button1; color: button1area.pressed ? "green" : "blue" } 
     } 
    ] 
} 
0

시도되지 않습니다 :

Rectangle { 
    id: button1 

    function toggle(){ 
     var tempstate = button1.state; //write var if is local variable 
     if (tempstate=="on") {state = "off"; } else { state = "on"} 
    } 

    MouseArea { 
    anchors.fill: parent 
    onPressed: parent.state = "pressed" 
    onReleased: parent.toggle() 
} 

"tempstate"국가의 이름을 들고 끈. 객체 참조가 없습니다. 함수의 state 속성에 직접 액세스 할 수 있습니다.

+0

이것은 게시 이후로 온 것입니다. 그러나'toggle() '을 호출 할 때 상태가 이미'pressed'로 변경되고 이전의 on/off 상태가 손실되기 때문에 이것은 작동하지 않습니다. 더 가깝지만 여전히 작동하지 않습니다. – Wolf

1

당신이 버튼을 사용하여 생각 해 봤나? 확인할 수있어 on/off 상태와 같으며 버튼이 현재 눌려져 있는지를 나타내는 'pressed'속성이 있습니다.

import QtQuick.Controls 1.3 
Button { 
    checkable: true 
    text: pressed ? "pressed" : (checked ? "on" : "off") 
}