我希望ApplicationWindow有一个自动隐藏menuBar,当鼠标光标位于窗口的最上部时显示.这在QML中是否可行?
PS:我正在使用Qt 5.3.
提前致谢.
您可以利用内部属性,即以"__"开头的属性.由于它们是内部的,即使IMO在这种情况下不太可能,在未来的版本中功能也可能会中断.
通过使用内部属性,您可以利用它__contentItem的图形容器MenuBar并为其属性设置动画以实现所需的结果.这是一种可能的方法; 它适用于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
}
}
}
Run Code Online (Sandbox Code Playgroud)
总结代码:
MouseArea定义了两个:ma1它覆盖了哪个MenuBar和ma2哪个填充了ApplicationWindow.后者有一个z负面定位在窗口的任何其他元素下面,包含ma1:这样它不会干扰与添加的任何元素相关的事件(例如示例按钮b1).ma1设置一个所谓的财产hideValue来1而ma2带来回0.该属性用于在一个视觉特性__contentItem(见(1),(2)并且(3)在代码),以隐藏/显示MenuBar.简单Behaviour的hideValue财产确保过渡顺利.__dismissMenu()用于确保Menu在MenuBar失去焦点时关闭打开.MenuItem触发a时,将hideValue直接重置属性以确保正确隐藏.