You are not logged in.

1

Saturday, March 31st 2012, 10:43am

QProcess doesn't get output text until the end of the process.

Hi, I'm new in Qt programming and I've and issue using QProcess class.

I've to build a GUI for a C console program. I need to be more modular possible, so I thought to build the GUI based on the use of QProcess, redirecting stdin/stdout on the GUI.
I tryed with a simple helloworld.c program

#include <stdio.h>
int main(void){
int number;
printf("Hello! Write a number:\n");
scanf("%d",&number);
printf("You wrote %d", number);
return 0;
}

the I wrote the program:


proc= new QProcess(this);
connect(proc, SIGNAL(readyReadStandardOutput()),this, SLOT(rightMessage()) );
connect(proc, SIGNAL(readyReadStandardError()), this, SLOT(wrongMessage()) );

proc->start("./helloworld", QIODevice::ReadWrite);


void tesQProcess::on_btnProcess_clicked()
{
QString str_command;
str_command = ui->lineeCommand->text();
proc->write(str_command.toAscii());
}


btnProcess is defined in the .ui file.

The program should start helloworld, print into my GUI "Hello! Write a number:", then I should write a number into lineCommand and press the button to see the final print: You wrote 6.

When I run the program, I get no initial output (Hello! Write a number: ).
Only after I write the number and the program linked to QProcess ends I get all the output (Hello! Write a number: Your wrote 6).
Am I missing something? How could I get sinchronized output?
Thanks for you help and sorry for my bad english

2

Saturday, March 31st 2012, 1:18pm

there is no end-line on your write().

this kind of thing works for me:

Source code

1
2
proc->write(str_command.toAscii());
proc->write("\n");
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

3

Saturday, March 31st 2012, 3:00pm


there is no end-line on your write() ...

Source code

1
scanf( "%d", &number ); // does not require end of line

better would be

Source code

1
scanf( " %d ", &number );

which surely will not solve the problem

PS:
in helloworld

Source code

1
2
3
printf( "Hello! Write a number:\n" );
scanf( " %d",&number );
printf( "You wrote %d\n", number ); // <<<<<<<<< added end of line


PPS:
seems to be a problem of buffered io, try (in helloworld)

Source code

1
2
3
setvbuf( stdout, NULL,_IONBF, 0 );
// or
setvbuf( stdout, NULL, _IOLBF, 0 );

This post has been edited 5 times, last edit by "Nicolas SOUCHON" (Mar 31st 2012, 3:16pm)


4

Saturday, March 31st 2012, 4:09pm

pray tell, how is scanf going to get anything from the buffer if you only type e.g. '5' instead of '5 <RETURN>'

Quoted


better would be
scanf( " %d ", &number );

No, that is no different at all, since white space in scanf format is ignored.

better would be to not use such rubbish as printf and scanf. try some c++! ;)
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

This post has been edited 2 times, last edit by "Amleto" (Mar 31st 2012, 4:17pm)


5

Sunday, April 1st 2012, 11:42am

I apologize for

Source code

1
2
// better would be
scanf( " %d ", &number );

that what I used to scan buffers (sscanf)
after verification, it does not work with scanf

I'm surprized

Quoted from "man scanf"


...
A directive is one of the following:
ยท A sequence of white-space characters (space, tab, newline, etc.; see isspace(3)). This directive matches any amount of white space, including none, in the input.
...

for new line, it works without new line char if output file is not buffered

6

Sunday, April 1st 2012, 1:51pm

http://www.cplusplus.com/reference/clibrary/cstdio/scanf/
Whitespace character: the function will read and ignore any whitespace characters (this includes blank spaces and the newline and tab characters) which are encountered before the next non-whitespace character. This includes any quantity of whitespace characters, or none.

In any event, there is no need to use such old and relatively unsafe methods.

iostream header should be much preferred over cstdio/stdio.h
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

7

Tuesday, April 3rd 2012, 11:02pm

Thank you everybody for your help! Nicolas you are right, it seems to be a problem about buffered stdout.
Using fflush(stdout) after printf() or simply setting setvbuff to 0 solves everything!

thanks guys ;)