-3

Hello everyone i have to implement MVC based app. How to load sparate exe file on QML form.

jay
  • 37
  • 2
  • 8

1 Answers1

1

What means MVC in this case? What mean load separate exe?

If you want to run another application from a QML ui you need a c++ interface / object which can do that. 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"
            }
        }
    }
}
Tim Jenßen
  • 903
  • 6
  • 5