2

I am writing an avrdude helper program that facilitates calling a few command-line arguments. When issuing avrdude commands in the console a series of output results will be displayed on the screen. Is there anyway to grab that output and display it in a GUI window (using Qt, if that matters)?

I figured I could take the output and redirect it to a file (avrdude -args > textFile.txt) that could be read and displayed on the screen at runtime if nothing else.

Just wondering if there are any other alternatives to capturing this output.

László Papp
  • 51,870
  • 39
  • 111
  • 135
sherrellbc
  • 4,650
  • 9
  • 48
  • 77

2 Answers2

3

I think the key classes are QProcess and QLabel or some similar GUI widget here as follows:

QProcess avrDudeProcess;
avrDudeProcess.setProcessChannelMode(QProcess::MergedChannels);
avrDudeProcess.start("avrdude", optionList);
if (!avrDudeProcess.waitForStarted())
    return false;

if (!avrDudeProcess.waitForFinished())
    return false;

QByteArray output = avrDudeProcess.readAll();
myLabel.setText(output);
László Papp
  • 51,870
  • 39
  • 111
  • 135
  • Very nice. `.. explicitly read all data from either of the two channels [stderr, stdout] by calling readAllStandardOutput() or readAllStandardError().` – sherrellbc May 24 '14 at 02:45
  • @sherrellbc: or setProcessChannelMode if you need both as per my updated answer. – László Papp May 24 '14 at 02:49
2

Maybe, that is what you are looking for.

http://linux.die.net/man/3/popen

That is an exmaple:

/* First open the command for reading. */
FILE * file = popen("/bin/ls /etc/", "r");

char output[100];
/* Read the output line by line */
while (fgets(output, 100, file) != NULL) 
{
    printf("%s", output); /* show the result */
}

/* close */
pclose(file);

Good luck!

Thalles
  • 272
  • 3
  • 14
  • I think you missed the fact that the OP was looking for a QT solution. `"... and display it in a GUI window (using Qt, if that matters)?"` – Jonathon Reinhart May 24 '14 at 03:07
  • @JonathonReinhart: was it you downvoting my answer? Interestingly enough, both answers got a -1 simultaneously and mine did not get any explanation. – László Papp May 24 '14 at 03:08
  • @LaszloPapp It wasn't me who down-voted anyone (I actually +1'd yours). This one does miss the whole QT thing, which is why I commented, but didn't downvote. I will usually comment with "-1" on down-voted answers. – Jonathon Reinhart May 24 '14 at 03:10
  • @JonathonReinhart: you are a fair contributor in my eyes then. That makes me happy. I get lots of unexplained downvotes (99%) and that does not help improving the posts. Perhaps the C tag misled Thalles. I think the OP could omit that. – László Papp May 24 '14 at 03:11
  • @LaszloPapp Agreed. I removed the C tag. – Jonathon Reinhart May 24 '14 at 03:13
  • Yes that is right, I was thinking in C. But I am glad you've found the answer you were looking for.@JonathonReinhart – Thalles May 24 '14 at 03:45