-2

I am trying to run this command and store the output in QString and display it using cout, however it does not work...

  QString str_command1;
  str_command1 = "netstat -i";

  proc1 = new QProcess();
  proc1->start(str_command1);

  QString tx;
  tx = proc1->readAllStandardOutput();
  std::cout << tx.toStdString() << std::endl;
krisdigitx
  • 7,068
  • 20
  • 61
  • 97
  • didn't see the original post on SO. Don't go all crazy on the downvote button. – UmNyobe Jun 20 '14 at 10:08
  • 1
    @UmNyobe: it is needed for people to realize not to answer frequently asked questions and just vote on close them or flag it. Otherwise people answer trivial and many times answered questions for the reputation or at least they do not know about the duplicate handling system if not for rep. Gold badge holders could close it asap, but we do not have any in the qt tag, sadly ;-) – László Papp Jun 20 '14 at 10:11
  • well let me get my qt gold badge ;) – UmNyobe Jun 20 '14 at 10:14
  • I would give you one if it was up to me. :) – László Papp Jun 20 '14 at 10:15

1 Answers1

-1

you should read the standard output until the process effectively terminates. Try something like:

if (proc1.waitForStarted(-1)) {
    while(proc1.waitForReadyRead(-1)) {
        tx += proc1.readAllStandardOutput();
    }
}
Leonardo Bernardini
  • 1,076
  • 13
  • 23
  • why busy wait when you have the signal `readyReadStandardOutput()` – UmNyobe Jun 20 '14 at 10:05
  • @UmNyobe: it is a duplicate, but I also prefer showing simple examples and the busy wait is simpler to demonstrate the issue: not waiting for the process to finish. No timeout is an issue though if the process hangs for some reason. – László Papp Jun 20 '14 at 10:06
  • how do you catch the signal if you're spawning and checking the output under the same function ? – Leonardo Bernardini Jun 20 '14 at 10:06
  • @LeonardoBernardini: learn more about Qt signals and slots, especially with lambdas. – László Papp Jun 20 '14 at 14:08
  • You can use lambdas and signals as long as you have an object on the heap, if you want it on the stack and do everything into the same block as the user asked, i really cannot catch a way to handle what you're proposing unless you force an event loop... IF there's such way , please show me the light ! – Leonardo Bernardini Jun 21 '14 at 12:21