PDA

View Full Version : QProcess communication



bunjee
22nd January 2009, 09:33
Hey there QtCenters,

I'm trying to achieve the following:

- Run a Qt app : myApp.
- Run a QProcess inside myApp : myProcess.
- Send a message "Ping" from the myApp toward myProcess.
- Answer "Pong" from myProcess.
- I want this to work on all three platforms on Qt 4.4.3.

What's the best way to do it ?

Do you have a small sample about this ?

Thanks.

wysota
22nd January 2009, 10:55
QProcess is a QIODevice - you can read from it and write to it. Whatever you write will end up in the process's STDIN and whatever it outputs to STDOUT (or STDERR) will be available to read from the QProcess object.

bunjee
22nd January 2009, 11:28
Sounds good,

So it means I have to trigger a QTimer to check the STDIN of my QProcess to grab the "ping".

Or is there an automated events for that?

^NyAw^
22nd January 2009, 11:31
Hi,

You can connect "readyRead" signal from QProcess to "readData" slot on your class.

bunjee
22nd January 2009, 11:43
How am I supposed to retrieve QProcess instance in myProcess app ?

^NyAw^
22nd January 2009, 11:52
Hi,

If I understand your request: You can use "yourProcess->write("ping")"(or similar) and then, if you have connected the signal that I told you, you will get the output messages of the process to your application.

Is this what you want?

bunjee
22nd January 2009, 12:00
I was talking about the STDIN reading implementation in the myProcess application:

I have found this:
http://www.qtforum.org/index.php?page=Thread&postID=90066&701d9f95#post90066

what do you think?

^NyAw^
22nd January 2009, 12:15
Hi,

"ping"
myApp -----------------------------> myProcess
|
|
| "do some work"
|
|
myApp <-----------------------------------
"message"

Is this what yoy want?

So if it's what you are expecting, why don't try the solution that I've posted?
"readyRead" signal will be emitted when "yourProcess" write to STDOUT and "yourSlot" will get this message.

bunjee
22nd January 2009, 13:38
No I want this:

"ping" myApp -----------------------------> myProcess <Do something>
"pong" myProcess ------------------------> myApp <Do something>

wysota
22nd January 2009, 14:52
Your process has to read commands from its standard input and write responses to its standard output. The way it does that is out of the scope of this question as it highly depends what the worker application is written in. For example you can use select() or poll() if available or a socket notifier if using Qt with an event loop.