PDA

View Full Version : No Interactive feel-QProcess



prasann87
9th January 2008, 09:16
Hi guys....
Im usin Qt 3.3.8 for designing a GUI based editor for C/C++ programs.
I use QProcess for running the program
For reading output I use readLineStdout().It all works fine .But im not able to get the feel of Interaction---the program simply goes on getting input and then finally it prints the otput.....I'll explain u my situation...

This is what I expect when I run a program:

Program:Enter the number of strings
myself: 2
Prog:Enter the string 1
my:aaa
Prog:Echoed string:aaa
Prog:Enter the string 2
my:bbb
Prog:Echoed string:bbb

but this is what happens when I run the program:

Prog: [ Nothin appears(at this instant) in the Text edit thats designd for showing the output ]
my:2
my:aaa
my:bbb
Prog:Enter the string 1
Echoed string:aaa
Enter the string 2
Echoed string:bbb [all of these appear at a single instance]

Now this is vat is my problem....
Great ones shall HELP me
PLZZZZZZZZZZZZZZZZZZZ

wysota
9th January 2008, 12:26
The interaction has to be provided by you. You have to read the data from the process, then allow user to do something and then write some data back to the process, etc. You should use signals and slots to obtain information about new data available to read and then do the reading. Instead you are probably reading everything in one go somewhere in your app. If not, please provide some code as now this is just guessing what you did.

prasann87
10th January 2008, 04:00
I' ve handled signals and slots and all but still there is the problem...
well here is the code..

proc = new QProcess( this );
proc->setCommunication( QProcess::Stdin |QProcess::Stdout|QProcess::Stderr|QProcess:big grin upStderr );
proc->addArgument( "./a.out" );
connect( proc, SIGNAL(readyReadStdout()),this, SLOT(readFromStdout()) );

if ( !proc->start() ) {
}
if(proc->isRunning()) {
proc->writeToStdin(InputLineEdit->displayText());
}

void MainForm::readFromStdout()
{
while(proc->canReadLineStdout()) {
OutputEdit->append( proc->readLineStdout() +'\n');
}
}

wysota
10th January 2008, 11:59
What about reacting to the output received and feeding user's response back to the process? I don't see it in your code.

prasann87
11th January 2008, 04:21
why it is there in the readFromStdout()...[here i get the process' o/p and put it in a TextEdit] and also the InputLineEdit->displayText()..[here i get the i/p from the lineedit and give it to the writeToStdin()]


1.
proc = new QProcess( this );
2.
proc->setCommunication( QProcess::Stdin |QProcess::Stdout|QProcess::Stderr|QProcess:big grin upStderr );
3.
proc->addArgument( "./a.out" );
4.
connect( proc, SIGNAL(readyReadStdout()),this, SLOT(readFromStdout()) );
5.

6.
if ( !proc->start() ) {
7.
}
8.
if(proc->isRunning()) {
9.
proc->writeToStdin(InputLineEdit->displayText());
10.
}
11.

12.
void MainForm::readFromStdout()
13.
{
14.
while(proc->canReadLineStdout()) {
15.
OutputEdit->append( proc->readLineStdout() +'\n');
16.
}
17.
}

wysota
11th January 2008, 10:47
In readFromStdOut() you only read and present the data back to the user, not write anything back... If the flow is not what you expect then that's because you are not controlling it enough. You should only allow your user to enter data if the backend process tells you so. Otherwise you'll always have the same problem, because if you input data, it gets queued in a buffer and then the application reads from the buffer, not knowing if the data got there before or after sending a prompt. You'd have to clear the buffer after sending the prompt to be certain of that.

So bottom line, you need a slot that will send the data to the process after receiving some data from the process. Because currently you only write something when you start the process (at least that's what the code you have shown does).