2017-06-06 7 views
0

私に1つ説明してください。アイテムがあるとします。アイテムをクリックすると、ドロップダウンメニューが表示されます。どのようにメニュー項目の上にマウスを置いたときに、それらのように目立つようにするには?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は次のようになります。この実装は、Qtので提供default implementationに基づいていることを

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 
      } 
     } 
    } 
} 

注:

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

を助けをありがとうございました。それは仕事です! –

関連する問題