如何从qml启动.exe文件?

时间:2016-11-21 10:13:09

标签: model-view-controller qml qt5.7 qqmlcomponent qqmlapplicationengine

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

1 个答案:

答案 0 :(得分:1)

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

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

#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"
            }
        }
    }
}