Is this an efficient use of QProcess?
-
waitForFinishedhas a timeout with a default value but in this case, since the process is not even running it will return early. -
I wonder if the confusion is that I left out the beginning code, the first process starts out:
QProcess *process = new QProcess(this); process->execute(file1,args1); process->waitforFinish(); process->close(); process = new QProcess(this); process->execute(file2,args2); process->waitforFinish(); process->close(); process = new QProcess(this); process->execute(file3,args3); process->waitforFinish(); process->close();I get no compile errors, and it's working. Per the Qt documentation close() deletes the object, so I don't believe I'm creating a memory leak with what I'm doing.
However, if I can eliminate creating a new object and then deleting it every time, I'm for improving the code.
-
waitForFinishedhas a timeout with a default value but in this case, since the process is not even running it will return early. -
@graniteDev Warning doesn't mean error unless you enabled "all warning be errors" for your compiler. As for close, it doesn't delete anything. It will kill the process that you started and close the communication channels. It's unrelated to memory management.
@JonB
waitForFinishedwill return false because of that. -
@graniteDev Warning doesn't mean error unless you enabled "all warning be errors" for your compiler. As for close, it doesn't delete anything. It will kill the process that you started and close the communication channels. It's unrelated to memory management.
@JonB
waitForFinishedwill return false because of that.@SGaist Ok, how should I go about this properly then? Could you provide some example code?
-
QProcess::execute(file1, args1); -
@SGaist I could run these from a static function? How then do I know when they are finished if there is no object to emit the signal?
-
Did you read the documentation of the function ?
executeruns the command and waits for it to finish. The returned value indicates what happened. -
Did you read the documentation of the function ?
executeruns the command and waits for it to finish. The returned value indicates what happened.@SGaist yes but if i just run QProcess::execute(file1, args1); there is no object there for me to deal with.
-
What do you want to deal with ? In the code you shown until now, what you are doing with your QProcess objects is already done "internally" by
execute. -
What do you want to deal with ? In the code you shown until now, what you are doing with your QProcess objects is already done "internally" by
execute.@SGaist never mind, I misunderstood because I'm not used to dealing with these. I was concerned that the subsequent processes could be started before the previous one finished, however execute is not asynchronous like start() is if I understood the documentation correctly.
-
It is indeed not asynchronous.
-
@SGaist ok well thankyou, I will give this a try.
-
@SGaist Worked. Wow is that much much simpler.
If I may though, for my future reference, if I needed to actually use a QProcess *process = new QProcess(this); to run multiple things, how would I go about it?
-
If in a fashion similar to
executesee the Synchronous Process API in QProcess's documentation. -
@SGaist Worked. Wow is that much much simpler.
If I may though, for my future reference, if I needed to actually use a QProcess *process = new QProcess(this); to run multiple things, how would I go about it?
@graniteDev said in Is this an efficient use of QProcess?:
If I may though, for my future reference, if I needed to actually use a QProcess *process = new QProcess(this); to run multiple things, how would I go about it?
Exactly as per your original code, except change
execute()tostart().