Qt QML:键盘和鼠标不能一起更改源图像

Qt QML:键盘和鼠标不能一起更改源图像,qml,Qml,我正在努力解决一个问题,我想它不是那么难,但我无法解决它。我在QML方面的经验非常少。我将感谢你的帮助 我有三个单选按钮作为图像。当我按下键时,焦点在单选按钮之间移动,因此按钮高亮显示。(当单选按钮的焦点更改时,源图像也会更改,因此具有焦点的单选按钮将用其他图像高亮显示) 问题:当我与鼠标交互时(请参阅源代码),源(图像)不再更改…不知道…而在鼠标交互之前源正在更改。我在调试器中检查了源代码行,鼠标交互后从未到达源代码行 我想这不是改变源图像的正确方法…请帮助我解决它或给我一个替代方案的建议 R

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

                   }
               }
           }
       }
    }


  }

请注意,您在这里使用的是
,而不是赋值运算符
=
-

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:
           {

           }
       }
}     
详细阅读