Simplify syntax by a lot

master
Kuba Orlik 2 years ago
parent 0c510483b8
commit 84306dcca0

@ -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>("Process", 1, 0, "Process");
```
Use in QML:
```
import Process 1.0
also, add the `SimpleProcess.qml` and `SimpleProcess.js` files to your
project.
# ...
Component.onCompleted: {
process.start("some-bin", ["some args"])
}
The simplest way to use it is to call the `callProcess` function:
```js
import "SimpleProcess.js" as SimpleProcess
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()
Item {
Component.onCompleted {
SimpleProcess.callProcess("echo", ["Hello", "World"], (_, stdout)=>console.log(stdout))
}
}
```

@ -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);
}

@ -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)
}
}
Loading…
Cancel
Save