How to redirect stdout/etc of detached QProcess
-
Hey
I think I've tried everything but still no luck.
I start my app as
QProcess *s = new QProcess() s->setProgram("Path") s->setArguments({"-arg=lala"} s->setProcessChannelMode(QProcess::ForwardedChannels); s->startDetached()
and I connect signals to >
readyReadStandardError readyReadStandardOutput
But no matter which setProcessChannelMode I pick, everything always goes in to my main application std out and not my functions/signal/slots. How can I properly redirect this?
TIA!
-
out of curiosity, do you connect the file descriptor signals BEFORE starting the process?
-
@Dariusz said in How to Re-dirrect stdout/etc of Detached QProcess:
How can I properly redirect this?
Don't start the process as detached...
-
@Dariusz said in How to Re-dirrect stdout/etc of Detached QProcess:
s->startDetached()
You are choosing to use bool QProcess::startDetached(qint64 *pid = nullptr). As a result
s->setProcessChannelMode(QProcess::ForwardedChannels);
has no effect, and the signals will not be raised. The documentation page covers this.By far the easiest thing you can do is not use
startDetached()
, as @Christian-Ehrlicher says, and usestart()
instead. Why do you need to usestartDetached()
? "Detached" means detached from the calling process so that it is not the child process's parent, and hence cannot access stiff like child's stdout/stderr, yet that seems to be exactly what you do want to access.... -
Hey
Yeh found it few hours latter. I ended up spawning QThread and running the process in that thread & spawning as ->start() instead. The startDetaching make sense, kinda wish id had more "backend" logic to handle streams/etc. I cant even disable stdout stream using the detached mode.
-
@Dariusz said in How to Re-dirrect stdout/etc of Detached QProcess:
I cant even disable stdout stream using the detached mode.
That's why it's called 'detached' ...
-
@Dariusz said in How to Re-dirrect stdout/etc of Detached QProcess:
I ended up spawning QThread and running the process in that thread & spawning as ->start() instead.
That sounds quite wrong. There is no need for any thread here,
QProcess::start()
and all the signals are asynchronous, what do you want any thread for?? -
@JonB said in How to Re-dirrect stdout/etc of Detached QProcess:
@Dariusz said in How to Re-dirrect stdout/etc of Detached QProcess:
I ended up spawning QThread and running the process in that thread & spawning as ->start() instead.
That sounds quite wrong. There is no need for any thread here,
QProcess::start()
and all the signals are asynchronous, what do you want any thread for??I don't want to block the main thread.
I want to run the app and then manage it from my GUI while it runs. -
@Dariusz said in How to Re-dirrect stdout/etc of Detached QProcess:
I don't want to block the main thread.
QProcess is async as @JonB already said so why do you block the main thread when you run a QProcess there?
-
@Dariusz said in How to Re-dirrect stdout/etc of Detached QProcess:
I don't want to block the main thread.
And? I know that. Like I said,
QProcess::start
/startDetached()
do not block anything. As I wrote, they are already asynchronous, and there is no need for any threading of your own.