diff --git a/README.md b/README.md index a0f83a5..2e8998a 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ Inspired by http://www.xargs.com/qml/process.html +## Setup + Add to project.pro: ``` @@ -17,38 +19,17 @@ Add to main.cpp qmlRegisterType("Process", 1, 0, "Process"); ``` -Use in QML: +also, add the `SimpleProcess.qml` and `SimpleProcess.js` files to your +project. -``` -import Process 1.0 - -# ... - - Component.onCompleted: { - process.start("some-bin", ["some args"]) - } - - - Text { - id: output - x: 0 - y: 0 - text: "" - } - - - Process { - id: process - //onReadyRead: {output.text += readAll();output.text += readAllStandardOutput();} - onReadyReadStandardError: {output.text += readAllStandardError();} - onFinished: { - console.log("finished", exitCode) - output.text += exitCode.toString() - console.log(readAllStandardError()); - } - onErrorOccurred: { - console.log("error!"); - output.text += readAllStandardError() - } - } +The simplest way to use it is to call the `callProcess` function: + +```js +import "SimpleProcess.js" as SimpleProcess + +Item { + Component.onCompleted { + SimpleProcess.callProcess("echo", ["Hello", "World"], (_, stdout)=>console.log(stdout)) + } +} ``` diff --git a/SimpleProcess.js b/SimpleProcess.js new file mode 100644 index 0000000..d0c0d6c --- /dev/null +++ b/SimpleProcess.js @@ -0,0 +1,12 @@ +function callProcess(binary, arguments, callback) { + const component = Qt.createComponent("SimpleProcess.qml"); + if (component.status != Component.Ready) { + if (component.status == Component.Error) { + console.debug("Error:" + component.errorString()); + } + throw new Error("Error:" + component.errorString()); + } + const process = component.createObject(parent, {}); + process.onHasFinished.connect(callback); + process.run(binary, arguments); +} diff --git a/SimpleProcess.qml b/SimpleProcess.qml new file mode 100644 index 0000000..74cc190 --- /dev/null +++ b/SimpleProcess.qml @@ -0,0 +1,43 @@ +import QtQuick 2.0 +import Process 1.0 + +Process { + property string binary + property variant arguments + + signal hasFinished(exitCode: int, stdout: string, stderr: string) + signal hasSuccess(stdout: string, stderr: string) + signal hasError(exitCode: int, stdout: string, stderr: string) + signal stdout(stdout_chunk: string) + signal stderr(stderr_chunk: string) + + property string entire_stdout: "" + property string entire_stderr: "" + + id: process + + function run(_binary: string, _arguments: variant){ + if(typeof _arguments == "string"){ + _arguments = [_arguments] + } + binary = _binary + arguments = _arguments + process.start(binary, arguments) + } + + onReadyRead: { + let stdout_chunk = readAllStandardOutput() + entire_stdout += stdout_chunk + process.stdout(stdout_chunk) + } + + onFinished: { + entire_stderr = readAllStandardError() + if (exitCode === 0) { + process.hasSuccess(entire_stdout, entire_stderr) + } else { + process.hasError(exitCode, entire_stdout, entire_stderr) + } + process.hasFinished(exitCode, entire_stdout, entire_stderr) + } +}