QProcess.startDetached () and write to stdin

What is wrong with this code? I cannot write the stdin of a new process that has been disconnected. It is important for me that the new process is disconnected.

QProcess commandLine; commandLine.setWorkingDirectory("E:\\"); //does not work. commandLine.startDetached("cmd.exe"); //works (but uses wrong working dir). commandLine.write("echo hi\0"); //writes nothing. commandLine.write("\n\r"); //Still nothing is written! 
+4
source share
2 answers

Good morning.

The problem is that QProcess::startDetached() is a static method that creates a fire and oblivion process.

This means that you cannot set the working directory this way. All you can do is call

 QProcess::startDetached (const QString &program, const QStringList &arguments, const QString &workingDirectory); 

This, however, leaves you with the problem of writing to the stdin the process you just created. The fact is that since you do not have a QProcess object, you cannot write your stdin. There may be a solution using the process descriptor of the static startDetached() method.

We had a similar problem in our company. We need separate processes that go beyond the duration of the calling program and for which we could establish an environment. It seemed, looking at the Qt code, impossible.

My solution was to use a wrapper around QProcess with my own startDetached() method. What he did, he actually created this subclass of QProcess on the heap and used his usual start() method. However, in this mode, the signal that is triggered after the process ends causes a slot that deletes the object itself: delete this; . It is working. The process runs independently and we can set up the environment.

Thus, in principle, there is no need to use the delayed start method. You can use the usual start method if your QProcess is an object on the heap. And if you care about memory leaks in this scenario, you will have to provide a similar mechanism as described above.

Regards, D

+7
source

calling a static method with arguments does not provide any set of processes to the child command.

 process.startDetached(command) 

try the following:

 QProcess process; process.setProgram(fileName); process.setArgument(argsList); process.setWorkingDirectory(dirName); process.startDetached(); 
0
source

All Articles