Skip to content

Instantly share code, notes, and snippets.

@ksvbka
Created March 4, 2020 07:42
Show Gist options
  • Save ksvbka/903df4745b684b10b2c2268c69eb9731 to your computer and use it in GitHub Desktop.
Save ksvbka/903df4745b684b10b2c2268c69eb9731 to your computer and use it in GitHub Desktop.
Get output from stdout when execute a command
import QtQuick 2.0
import QtQuick.Controls 1.0
import QtQuick.Layouts 1.0
import org.kde.plasma.core 2.0 as PlasmaCore
Item {
implicitWidth: label.implicitWidth
implicitHeight: label.implicitHeight
property string data: "?"
PlasmaCore.DataSource {
id: executable
engine: "executable"
connectedSources: []
onNewData: {
var exitCode = data["exit code"]
var exitStatus = data["exit status"]
var stdout = data["stdout"]
var stderr = data["stderr"]
exited(exitCode, exitStatus, stdout, stderr)
disconnectSource(sourceName) // cmd finished
}
function exec(cmd) {
connectSource(cmd)
}
signal exited(int exitCode, int exitStatus, string stdout, string stderr)
}
Connections {
target: executable
onExited: {
data = stdout.replace('\n', ' ').trim()
}
}
Label {
id: label
text: i18n("<b>ls -la\n:</b> %1", data)
}
Component.onCompleted: {
var cmd = 'ls -la'
executable.exec(cmd)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment