Running a bash script and outputting output to text box
-
Hello,
I wanted to know if it's possible to run a bash script in the background and read it's output into a text box?
UPDATE: So far, i have used QProcess to run the command "w" and log it's output via QDebug. How can i now send this to my text box in qml?@QProcess proc;
proc.start("/bin/sh");
proc.waitForStarted();
Q_FOREACH( QString cmd, QStringList() << "w" )
{
proc.write((cmd + "\n").toLocal8Bit());
proc.waitForReadyRead();
qDebug() << proc.readAllStandardOutput().split('\n');
}
proc.write("exit\n");
proc.waitForFinished();
@Thanks.
-
Hi,
You can expose a C++ functions to QML. Just Wrap the above code into a C++ class. Then using properties, methods and signals you can send the output to QML side.
Please go through this "doc":http://doc.qt.io/qt-5/qtqml-cppintegration-exposecppattributes.html carefully.Also waitFor* functions are blocking. Try using "readyReadStandardOutput()":http://doc.qt.io/qt-5/qprocess.html#readyReadStandardOutput instead.
Edited
-
Thanks. I've tried this with QObject and here's what i have:
The text is not updating...
-
Oddly enough, this works (just a different main.qml with a new project):
@import QtQuick 2.0Item { id: item1 width: 400 height: 400 property alias text1Text: text1.text Text { id: text1 width: 400 height: 29 color: "red" text: "This text should change..." font.pixelSize: 12 } }
@
Can you tell me why this works and the other one with a different design doesn't?
Thanks!
-
@
QObject *rootObject = view.rootObject();
rootObject->setProperty("text1Text",QVariant("Casdasdasda."));
@With earlier QML it didnt work because there's no such text1Text in rootObject which was MainView
And with the second QML, rootObject Item has this property.
To make it work with earlier QML, you can use use findChild to find the exact element by objectName.
You can find an example "here":http://doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfromcpp.html#accessing-loaded-qml-objects-by-object-name. -
Thanks, solved. One more thing, I've defined object as view.rootObject(); If i use this in my own class for a function, do i have to redefine view like this? It's not recognized in a class other than main.cpp.
@QQuickView view@
For anyone looking for a solution, here's what i did to resolve it:
@ QQuickItem* object = view.rootObject();
QObject *text1 = object->findChild<QObject*>("text1"); if (text1) text1->setProperty("text", "new text");
@
EDIT: Also, i would like to append each line from readAllStandardOutput() to the text Area, instead of just updating it's current text. The function i'm going for is like a log, it needs to display all output in the text area.
[quote author="p3c0" date="1421124837"]@
QObject *rootObject = view.rootObject();
rootObject->setProperty("text1Text",QVariant("Casdasdasda."));
@With earlier QML it didnt work because there's no such text1Text in rootObject which was MainView
And with the second QML, rootObject Item has this property.
To make it work with earlier QML, you can use use findChild to find the exact element by objectName.
You can find an example "here":http://doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfromcpp.html#accessing-loaded-qml-objects-by-object-name.[/quote] -
bq. One more thing, I’ve defined object as view.rootObject(); If i use this in my own class for a function, do i have to redefine view like this?
Not needed. You can just pass this object as parameter to that class.
bq. Also, i would like to append each line from readAllStandardOutput() to the text Area, instead of just updating it’s current text. The function i’m going for is like a log, it needs to display all output in the text area.
You can just emit a signal with its argument as the new outputted line and then use "append()":http://doc.qt.io/qt-5/qml-qtquick-controls-textarea.html#append-method of TextArea to append it.
-
[quote author="p3c0" date="1421150392"]bq. One more thing, I’ve defined object as view.rootObject(); If i use this in my own class for a function, do i have to redefine view like this?
Not needed. You can just pass this object as parameter to that class.
^ Can you give me an example of how to do this? Thanks.
bq. Also, i would like to append each line from readAllStandardOutput() to the text Area, instead of just updating it’s current text. The function i’m going for is like a log, it needs to display all output in the text area.
You can just emit a signal with its argument as the new outputted line and then use "append()":http://doc.qt.io/qt-5/qml-qtquick-controls-textarea.html#append-method of TextArea to append it.[/quote]
Thanks, got it.