【发布时间】:2023-03-05 14:33:01
【问题描述】:
我正在向我的 QtQuick GUI (as in here I believe) 添加一个弹出菜单,但它的行为与我预期的不同。
这是我的工作:
import QtQuick 2.7
import QtQuick.Layouts 1.3
import QtQuick.Window 2.2
import QtQuick.Controls 2.0
import QtQuick.Controls.Styles 1.2
ApplicationWindow
{
...
// File menu button.
Rectangle
{
id: ribbonFileMenuButton
anchors.right: parent.right
anchors.verticalCenter: parent.verticalCenter
width: height
height: parent.height
scale: ribbonFileMenuButtonMA.pressed ? 1.3 : 1
color: "transparent"
// Icon.
RibbonFileButtonIcon
{
id: ribbonFileMenuButtonIcon
anchors.fill: parent
}
// Behavior.
MouseArea
{
id: ribbonFileMenuButtonMA
anchors.fill: parent
onClicked: menu.open() /*popup()*/
}
}
...
// File.
Menu
{
id: menu
y: 20
MenuItem
{
text: "New..."
}
MenuItem
{
text: "Open..."
}
// MenuSeparator { }
MenuItem
{
text: "Save"
}
}
...
}
首先,我必须调用 menu.open() 而不是 menu.popup() (如上面提供的链接中所示的文档中所述):menu.popup() 输出错误:
TypeError: 对象 QQuickMenu(0x20f40f0) 的属性“弹出”不是函数
如果我取消注释 MenuSeparator { },我会收到以下错误:
MenuSeparator 不是类型
同样,根据提供的链接中的文档,它应该可以工作。
我上网查了一下,有点迷茫……
谢谢,
安托万。
【问题讨论】:
-
在“import QtQuick.Controls 1.3”下查看MenuSeparator是否可用?
-
嗯...这个回归回答了我的两个问题,这很奇怪...看起来 QtQuick.Controls 2.0 是一个尚未完全完成的完全重写...我说的对吗?
-
我不能肯定地说,但如果您说明您正在使用的 QT 的确切版本会有所帮助。我目前使用的是 5.4,那里的文档明确指出“MenuSeparator”可用于该 QT 版本和我告诉你尝试的 QtQuick.Controls 1.3 版本。如果您使用的是比我更新的 QT 版本(这很可能),那么很可能在 QT 版本之间对该 API 进行了更改。
-
使用代码中所述的 QtQuick.Controls 2.0(因此 Qt 5.7 但我应该明确提到它,你是对的)。我相信您的解释是正确的(并由@Mitch 确认)。非常感谢;)
标签: qt qml qtquick2 qt-quick qtquickcontrols2