2017-06-06 61 views
0

请有人解释我一件事。假设我有一个项目,如果我点击它,那么会出现一个下拉菜单。当你将鼠标悬停在菜单项上时,如何做到这一点?QML MenuItem突出显示不起作用

代码:

Rectangle { 
    id: main_window 
    width: 600 
    height: 600 
    property int mrg: 10 

    Rectangle { 
     anchors.centerIn: parent 
     width: 500 
     height: 500 
     color: 'green' 

     Text { 
      id: field 
      text: "Click!" 
      font.pointSize: 20 
      color: 'white' 
      anchors.centerIn: parent 

      MouseArea { 
       id: ma 
       anchors.fill: parent 
       hoverEnabled: true 

       onClicked: { 
        menu.x = ma.mouseX 
        menu.open() 
       } 
      } 

      Menu { 
       id: menu 
       y: field.height 
       MenuItem { 
        text: "Menu item" 
        highlighted: true 

       } 
      } 
     } 
    } 
} 

在本文档中,我碰到了合适的highlight负责选择相应的菜单项的地步。我将它安装在True中,但它没有改变任何东西。 请告诉我我做错了什么。非常感谢。

回答

1

MenuItem默认实现不包括任何视觉高亮显示功能,但可以作为Qt manuals解释图形表示适应您的需求。所以,你的MenuItem应该是这样的:

MenuItem { 
    id: control 
    text: "Menu item" 

    background: Item { 
     implicitWidth: 200 
     implicitHeight: 40 

     Rectangle { 
      anchors.fill: parent 
      anchors.margins: 1 
      color: control.highlighted ? "blue" : "transparent" // blue background if the control is highlighed 
      MouseArea { 
       anchors.fill: parent 
       hoverEnabled: true // enable mouse enter events when no mouse buttons are pressed 
       onContainsMouseChanged: control.highlighted = containsMouse // set the highlighted flag when the mouse hovers the MenuItem 
      } 
     } 
    } 
} 

注意,这个实现是基于Qt提供的default implementation

background: Item { 
    implicitWidth: 200 
    implicitHeight: 40 

    Rectangle { 
     x: 1 
     y: 1 
     width: parent.width - 2 
     height: parent.height - 2 
     color: control.visualFocus || control.down ? Default.delegateColor : "transparent" 
    } 
} 
+0

非常感谢您的帮助。是工作! –

相关问题