2014-12-04 98 views

回答

4

您可以利用内部属性,即以“__”开头的属性。由于它们是内部的,即使IMO在这种情况下不太可能发生,在未来的版本中功能也可能会中断。

通过使用内部属性,您可以利用__contentItemMenuBar的图形容器)并对其属性进行动画处理以实现所需的结果。这是一种可能的方法;它使用Qt 5.3/QT 5.4工作/ Qt的5.5(不包括我可以在测试它):

ApplicationWindow { 
    id: app 
    visible: true 
    width: 400 
    height: 300 

    property real hideValue: 0 
    Behavior on hideValue { 
     NumberAnimation {duration: 200} 
    } 

    menuBar: MenuBar { 
     id: menu 
     //__contentItem.scale: value      // (1) 
     //__contentItem.opacity: hideValue     // (2) 
     __contentItem.transform: Scale {yScale: hideValue} // (3) 

     Menu { 
      id: m1 
      title: "File" 
      MenuItem { text: "Open..." 
       onTriggered: { 
        hideValue = 0       // hide the bar 
       } 
      } 
      MenuItem { text: "Close" 
       onTriggered: { 
        hideValue = 0       // hide the bar 
       } 
      } 
     } 
    } 


    Button{ 
     id: b1 
     anchors.centerIn: parent 
     text: "CLICK ME!" 
     width: 100 
     height: 100 
    } 

    MouseArea { 
     id: ma1 
     anchors.top: parent.top 
     anchors.left: parent.left 
     anchors.right: parent.right 
     height: menu.__contentItem.implicitHeight 
     hoverEnabled: true 

     onEntered: { 
      hideValue = 1       // show the bar 
     } 
    } 

    MouseArea { 
     id: ma2 
     anchors.fill: parent 
     hoverEnabled: true 
     z: -1 

     onEntered: { 
      m1.__dismissMenu() 
      hideValue = 0       // hide the bar 
     } 
    } 
} 

总结代码:

  • 两个MouseArea定义为:ma1覆盖MenuBarma2填写ApplicationWindow。后者有一个z负面定位根据该窗口的任何其他元素,包括ma1:这种方式,它不能干涉与添加任何元素相关的事件(例如示例按钮b1)。
  • ma1设置一个属性,称为hideValue1ma2将它带回0。该物业用于__contentItem(参见代码中的(1),(2)(3))的视觉财产以隐藏/显示MenuBar。通过hideValue属性的简单Behaviour可确保转换顺畅。
  • 内部函数__dismissMenu()用于确保在MenuBar失去焦点时打开的Menu已关闭。
  • MenuItem被触发时,hideValue属性被直接重置以确保正确的隐藏。
1

我设法得到一些结果与此代码:

ApplicationWindow { 
    id: app 

    MenuBar { 
     id: menu 
     Menu { 
      title: "Menu 1" 
      MenuItem { 
       text: "item 1" 
      } 
      MenuItem { 
       action: "item 2" 
      } 
     } 
    } 

    MouseArea { 
     anchors.top: parent.top 
     anchors.left: parent.left 
     anchors.right: parent.right 
     height: 20 
     hoverEnabled: true 

     onEntered: { 
      if (app.menuBar === menu) 
       app.menuBar = null; 
      else 
       app.menuBar = menu; 
     } 
    } 
} 

的变化试图访问null.__contentItem当栏隐藏然而时突然和QML调试报告错误。当然,代码中的绝对大小可能会导致问题。