-2

大家好,我必须实现基于MVC的应用程序。如何在QML表单上加载单独的 exe文件。

4

1 回答 1

1

在这种情况下,MVC 是什么意思?加载单独的exe是什么意思?

如果你想从 QML ui 运行另一个应用程序,你需要一个可以做到这一点的 c++ 接口/对象。 主文件

#include <QGuiApplication>
#include <QQmlApplicationEngine>

#include <QProcess>
#include <QQmlContext>

class ProcessStarter : public QProcess {
    Q_OBJECT
public slots:
    void run(const QString &application) {
        startDetached(application);
    }
};

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;
    ProcessStarter starter;
    engine.rootContext()->setContextProperty("starter", &starter);
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    return app.exec();
}

#include "main.moc"

main.qml

import QtQuick 2.5
import QtQuick.Window 2.2

Window {
    id: window
    visible: true
    width: 200
    height: 200
    title: qsTr("Hello World")

    TextEdit {
        id: textEdit
        height: 20
        text: qsTr("Enter some path to a binary and click red area")
        anchors.right: parent.right
        anchors.left: parent.left
        verticalAlignment: Text.AlignVCenter
    }
    Rectangle {
        id: rectangle
        x: 0
        y: 20
        width: window.width
        height: window.height - 20
        color: "#d02626"

        MouseArea {
            id: mouseArea
            anchors.fill: parent
        }

        Text {
            id: text1
            anchors.centerIn: parent
        }

        Connections {
            target: mouseArea
            onClicked: {
                starter.run(textEdit.text)
                text1.text = textEdit.text + " started"
            }
        }
    }
}
于 2016-11-22T12:00:37.060 回答