PDA

View Full Version : Qprocess never end in MS windows



antonio.r.tome
21st February 2006, 17:57
Hi,
I'm trying to run some external programs fron a qt4.0 interface and catch the standardoutput and standard error.

I'm using Qprocesses class. Things work very good im Linux (Opensuse 10.0) but in windows (2000 Pro, qt4.1 open version) the external process never end and the finished sign is not emmitted.

my code is very easy, is a small fraction

QProcess nh3d;
Ui_MainWindow ui;
void batchprocess::nhdstart()
{

if(nh3d.state() ==0 )
nh3d.start("c:\\winnt\\system32\\mem");
else
{
qWarning("Program it's already running \n");
ui.textBrowser_2->append("Program it's already running \n");
ui.textBrowser->append(nh3d.readAllStandardOutput());
}
}

the function batchprocess::nhdstart() is connected to a click of a pushup button and it is working.

Any help or clues will be very appreciated.
I've found an old thread on the subject but the solutions there aren't good for me


best Regards

jacek
21st February 2006, 18:34
What does nh3d.start(...) return?

antonio.r.tome
21st February 2006, 18:53
What does nh3d.start(...) return?
I really don't know but the
SIGNAL(started( ))
is emmited because it is linked to a label that displays running after theexternal program start, and that is working fine, even in windows.

Subsequent clicks in the button will write in a textframe
"program is already running"
so
nh3d.state() is different from 0

the program necer ends.
if I choose a external program with a huge amount of output almost all the output is displayes in a textframe but the last three or four lines

jacek
21st February 2006, 19:23
How do you read that output? Maybe it stops becuse of full buffers?

antonio.r.tome
21st February 2006, 19:55
How do you read that output? Maybe it stops becuse of full buffers?

I read it by doing!
ui.textBrowser->append(nh3d.readAllStandardOutput());

and the program do not stop! It never ends

jacek
21st February 2006, 21:43
I read it by doing!
ui.textBrowser->append(nh3d.readAllStandardOutput());
Each time the readyReadStandard...() signal is emitted?

michel
21st February 2006, 23:53
Yes, you're reading all the standard output, but only one time. You need to connect the signal readyReadStandardOutput to a slot for using readAllStandardOutput.

Say the process you are running sends
"Blah blah blah blah blah
Blah blah blah blah blah
Blah blah blah blah blah
Blah blah blah blah blah
Blah blah blah blah blah"

to stdout. Your call to nh3d.readAllStandardOutput() picks it up, which is fine. But now say the process wants to say

"Blah blah blah blah blah" again and you are not checking to see if anything is there.

If you don't want to do signals and slots, and you don't need interactive use of the program until the other process finishes, you can use a blocking wait:



if (nh3d.state() ==0 )
nh3d.start("c:\\winnt\\system32\\mem");
else while (nh3d.state() == QProcess::Running)
{
if (nh3d.waitForReadyRead(MAXIMUM TIME IN MILLISECONDS YOU ARE WILLING TO WAIT FOR THE PROCESS TO SEND SOMETHING TO THE STREAM) == TRUE) dosomethingwiththedata;
else
decidewhattodoiftheprocessisn'tsendingyouanythinga fteralongtime,likekillingitandexiting;
}

Just make sure the while loop does not run forever :D. In this pseudocode, it probably would unless the program normally exits on its own.

antonio.r.tome
22nd February 2006, 09:56
[QUOTE=michel]Yes, you're reading all the standard output, but only one time. You need to connect the signal readyReadStandardOutput to a slot for using readAllStandardOutput.

I'm sorry I didn't sent you all the code!
I'm already reading the standard Output in a slot of a function connected to readyReadStandardOutput signal!

and as I said it works great in Linux.

I have the follwoing code aditional code.


.....
QObject::connect(&nh3d, SIGNAL(readyReadStandardOutput ()), &readwrite, SLOT(owrite()));
QObject::connect(&nh3d, SIGNAL(readyReadStandardError ()), &readwrite, SLOT(ewrite()));


readwrite is a class with the slots owrite e ewrite
.............................
void Readoutput::owrite()
{
ui.textBrowser->append(nh3d.readAllStandardOutput());
/
}
void Readoutput::ewrite()
{
ui.textBrowser_2->append(nh3d.readAllStandardError());





......................

Because this weird problem today I'm going to Update my windiows 2000 to XP to see if the problem solves by itself.

Best regards,

michel
22nd February 2006, 13:23
Hm. Weird. Maybe processes in Windows just don't behave correctly (that would sure be a surprise).

wysota
22nd February 2006, 13:40
Do those slots get called at all?

antonio.r.tome
22nd February 2006, 14:40
Do those slots get called at all?

Yes

if i put a command with large output. (I use nh3d an executable program of my one, where the standards erro and output are flushed every time they are used) I obtain the full output in stderr and the output of stdout behaves great but for the last two or three lines. It happens as the external processed freezed just before finished.

The example I put were I call mem was only to see if with a native windows application I got some output, but the behavior is the same.

I've just update for XP but the results are the same.

António Tomé

antonio.r.tome
23rd February 2006, 12:15
I've solved de problem:

In my example
QProcess nh3d

is definided as a global variable (C++ books are always alerting to avoid them ...)

Kwow I've defined
QProcess nh3d as a plublic variable of the class which contais the slot that starts the Qprocess and everthing works fine in linux and windows.

The remaining, and maybe unuseful, question is to know what are the right question:

Why it didn't work as was in windows? Or
why it work as it was in linux?

my best Regards and many thanks to everyone o tried to help

António Tomé

jacek
23rd February 2006, 12:35
Why it didn't work as was in windows? Or
why it work as it was in linux?
One possible explanation is that it was instantiated before QCoreApplication, which performes some platform-dependent initialization.