wlredeye
wlredeye

Reputation: 1014

how to redirect system output to my gui app (qt, linux)?

I need to develop a GUI program which will be run some external bash script. This script are working about 30-40 minutes and I want to see system output in my application in real time.

How can I provide this? Should I use QTextStream?

Please give me some examples.

Upvotes: 3

Views: 1734

Answers (1)

TheDarkKnight
TheDarkKnight

Reputation: 27611

If you launch the script via QProcess, you can get the output by connecting to the readyRead signal. Then it's just a matter of calling any of the read functions to get the data and then displaying it on any type of widget you want, such as a QTextEdit which has an append function for adding text.

Something like this: -

// Assuming QTextEdit textEdit has been created and this is in a class
// with a slot called updateText()
QProcess* proc = new QProcess;
connect(proc, SIGNAL(readyRead()), this, SLOT(updateText()));
proc->start("pathToScript");

...

// updateText in a class that stored a pointer to the QProcess, proc
void ClassName::updateText()
{
    QString appendText(proc->readAll());
    textEdit.append(appendText);
}

Now, every time the script produces text, your updateText function is called and you are adding it to the QTextEdit object.

Upvotes: 5

Related Questions