Qt QML:键盘和鼠标无法一起更改源图像

时间:2013-05-02 16:31:04

标签: qml

我正在努力解决问题,我想它不是那么困难,但我无法解决它。我对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:{

                   }
               }
           }
       }
    }


  }

1 个答案:

答案 0 :(得分:0)

请注意,您使用的是:,而不是分配运营商= -

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

这意味着您正在建立绑定,而不仅仅是为属性分配固定值。

所以,如果你有像

这样的东西
x : y

如果要更改属性“x”,而不是直接更改属性,请更改此属性“x”所依赖的属性“y”,或者绑定该属性。

对于你的情况 -

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