2013-05-02 27 views
0

我正在努力解决问题,我猜它不是那么难,但我无法解决它。我对QML的体验非常小。我会感谢你的帮助。Qt QML:键盘和鼠标不能一起改变源图像

我有三个单选按钮作为图像。当按下按键时,焦点在单选按钮之间移动,因此按钮突出显示。 (由于单选按钮的焦点更改,源图像也会更改,因此带有焦点的单选按钮将与其他图像一起高亮显示)。

问题:当我与鼠标交互时(见源代码),源代码(图像)不再改变...... ..不知道......在鼠标交互之前源发生变化。我在调试器中检查了鼠标交互后永远不会到达源代码行。

我想它不是正确的方式更改为源图像......请帮我解决这个问题或者给我一个建议的替代

Rectangle { //main container 
    id: rectangle1 
    x: 0 
    y: 0 

    width: 480 
    height: 620 
    color: "#ffffff" 
    Item { // focus scope container 
     id: focus_object 
     focus : true 

     Image { // radio button 1 
      id: rock 
      x: 5 
      y: 6 
      fillMode: Image.PreserveAspectFit 
      smooth: true 
      focus:true 
      source: focus ? "Radiobutton_unselected_highlighted.png" : "Radiobutton_unselected.png" 


      KeyNavigation.right: pop 


      MouseArea { 
       anchors.fill: parent 
       hoverEnabled: true 
       onEntered: { 
        parent.source = "Radiobutton_unselected_highlighted.png" 
       } 
       onExited: { 
        parent.source = "Radiobutton_unselected.png" 
       } 
       onClicked:{ 
       } 
      } 
     } 

     Image { // radio button 2 
      id: pop 
      x: 160 
      y: 6 
      width: 64 
      height: 64 
      fillMode: Image.PreserveAspectFit 
      smooth: true 
      source: focus ? "Radiobutton_unselected_highlighted.png" : "Radiobutton_unselected.png" 




      KeyNavigation.left: rock 

      KeyNavigation.right: classic 

      MouseArea { 
       anchors.fill: parent 
       hoverEnabled: true 
       onEntered: { 
        parent.source = "Radiobutton_unselected_highlighted.png" 
       } 
       onExited: { 
        parent.source = "Radiobutton_unselected.png" 
       } 
       onClicked:{ 

       } 

     } 
     Image { // radio button 3 
       id: classic 
       x: 306 
       y: 6 
       width: 64 
       height: 64 
       fillMode: Image.PreserveAspectFit 
       smooth: true 

       source : focus ? "Radiobutton_unselected_highlighted.png" : "Radiobutton_unselected.png" 
       KeyNavigation.left: pop 

       MouseArea { 
        anchors.fill: parent 
        hoverEnabled: true 
        onEntered: { 
         if (true == focus) 
         parent.source = "Radiobutton_unselected_highlighted.png" 

        } 
        onExited: { 
         parent.source = "Radiobutton_unselected.png" 
        } 
        onClicked:{ 

        } 
       } 
      } 
     } 
    } 


    } 

回答

0

请注意,您正在使用:,而不是分配运营商=这里 -

source: focus ? "Radiobutton_unselected_highlighted.png" : "Radiobutton_unselected.png" 

这意味着你正在建立一个绑定,不只是分配一个固定值的属性。

所以,如果你有类似

x : y 

当你想改变属性“X”,而不是直接更改属性,更改属性“Y”上这个属性“x”的依赖,或与...绑定在一起。

对于你的情况 -

Image 
{ // radio button 1 
     id: rock 
     x: 5 
     y: 6 
     fillMode: Image.PreserveAspectFit 
     smooth: true 
     focus:true 
     source: focus ? "Radiobutton_unselected_highlighted.png" : "Radiobutton_unselected.png" 

     KeyNavigation.right: pop 

     MouseArea 
     { 
      anchors.fill: parent 
      hoverEnabled: true 
      onEntered: 
      { 
       rock.focus = true 
      } 

      onExited: 
      { 
       rock.focus = false      
      } 

      onClicked: 
      { 

      } 
     } 
}  

请详细阅读有关qml property binding