【问题标题】:Qt QML: Keyboard and mouse does not work together to change the source imageQt QML:键盘和鼠标不能一起改变源图像
【发布时间】:2013-09-12 12:11:39
【问题描述】:

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

                   }
               }
           }
       }
    }


  }

【问题讨论】:

    标签: qml


    【解决方案1】:

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

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-18
      • 1970-01-01
      • 2016-11-06
      相关资源
      最近更新 更多