Qt QML - 将Qt Widgets和QML与QWidget :: createWindowContainer()结合使用时,QML中的动作快捷方式不起作用

时间:2015-11-16 08:53:00

标签: qt widget qml

我开始使用从QMainWindow和Qt Designer表单派生的UI类创建Qt GUI应用程序的示例。 在mainwindow.cpp中实现UI类时,我创建了一个QQuickView,然后调用QWidget :: createWindowContainer(),传入视图和父窗口小部件。这将返回我们可以添加到UI布局的QWidget。下面显示的是mainwindow.cpp中的代码。

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    setWindowTitle(tr("Tab Dialog"));

    _qmlView        = new QQuickView();
    _qmlView->setSurfaceType(QSurface::OpenGLSurface);
    _qmlView->setSource(QUrl("TMainView.qml"));
    _qmlView->setResizeMode(QQuickView::SizeRootObjectToView);
    _qmlWidget      = QWidget::createWindowContainer(_qmlView);

    ui->horizontalLayout->addWidget(_qmlWidget);
}

我创建了一个显示矩形的简单QML文件,当用户单击鼠标右键时,菜单将会出现。

Rectangle {
    id: main
    width: 600
    height: 300

    Rectangle{

        Menu {
            id: menu
            title: "Edit";

            MenuSeparator { }

            MenuItem { action: copyAction; shortcut: StandardKey.Copy }
            MenuItem { action: pasteAction }
            MenuItem { action: selectAllAction }

            MenuSeparator { }
            MenuItem { action: deleteAction }
            MenuItem { text: "Delete all" }

            MenuSeparator { }
            MenuItem { text: "Auto arrange" }

            }
    }

    Action {
            id: copyAction
            text: "&Copy"
            shortcut: StandardKey.Copy
            iconName: "edit-copy"
            onTriggered: console.log("Ctrl+C is pressed")
        }

    Action {
            id: pasteAction
            text: "&Paste"
            shortcut: StandardKey.Paste
            iconName: "edit-paste"
            onTriggered: console.log("Ctrl+V is pressed")
        }

    Action {
            id:selectAllAction
            text: "&SellectAll"
            shortcut: StandardKey.SelectAll
            iconName: "edit-selectAll"
            onTriggered: console.log("Ctrl+A is pressed")
        }

    Action {
            id: deleteAction
            text: "&Delete"
            shortcut: StandardKey.Delete
            iconName: "edit-delete"
            onTriggered: console.log("Delete is pressed")
        }

    MouseArea{
        anchors.fill : parent;
        acceptedButtons: Qt.LeftButton | Qt.RightButton

        onClicked: {
            if(mouse.button === Qt.RightButton){
                menu.popup();
            }
        }
    }
}

但我的问题是,当我单击鼠标右键时,快捷方式在此之后不起作用,但随后我最小化窗口,然后最大化快捷方式将再次激活。每当我单击鼠标右键时,快捷键将被禁用。为什么?

0 个答案:

没有答案