1

我正在向我的 QtQuick GUI 添加一个弹出菜单(就像我相信的那样),它的行为不像我预期的那样。

这是我所做的:

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 不是类型

同样,根据提供的链接中的文档,它应该可以工作。

我在互联网上看过,但我有点迷茫......

谢谢,

安托万。

4

1 回答 1

2

As @ManuelH said, MenuSeparator isn't available in Qt Quick Controls 2... yet. :)

The 2.0 version is indeed a complete rewrite that brings with it a new API. A lot of the same types are there, but the documentation should be followed closely to avoid relying on API or behaviour from Qt Quick Controls 1.x.

Source compatibility breaks are permitted (though attempted to be kept to a minimum) across major versions (e.g. QtQuick 1.0 to QtQuick 2.0, Qt 4 to Qt 5, etc.).

See this page for more information about the differences between the two APIs, and the blog post that it links to.

于 2016-07-22T15:28:26.053 回答