PDA

View Full Version : problem in QProcess



wagmare
2nd January 2009, 14:08
hi friends,
please help :(
i create a code to embed gtk window to my Qt app ..
mainwindow.cpp



procc2 = new QProcess();
procc2->setProcessChannelMode(QProcess::MergedChannels);
connect(procc2, SIGNAL(readyReadStandardOutput()), this, SLOT(readFromStdout2()));


this is a timer function calling every 400 milli sec


MainWindow::timeout{
if(...)
{
QString program4 = "./gtk_demo2";
QStringList arguments2;
if(procc2->state() != QProcess::Running)
{
procc2->start(program4, arguments2);
}
}


readfromstdout() function will call the function to embed the process


MainWindow::readFromStdout2()
{
int j = 0;
bool ok;
QString out;
printf("in singal fin\n");
out = procc2->readAllStandardOutput();
j = out.toInt(&ok, 16);
printf("j =%d\n",j);
if(j == 1)
printf("device not connected\n");
else{
printf("the device connected\n");
embed();
}

}


this is my code and the function embed() will embed the "gtk_demo" window if it returns 0 or anything ...
the problem is on first time if i spawn the signal "readyReadStandardOutput()"
is not emitted and readFromStdout2() slot is not called .
i closed the spawned gtk application "gtk_demo" and as i put the procc in loop again it spawn "gtk_demo" its emitting "readyReadStandardOutput()" and embedding perfectly ... why??:confused:
i changed gtk application to a Qt application and try the same thing its worked in the first attempt ... :confused:

please help me ...

wysota
2nd January 2009, 17:44
Did you check the value of "ok" variable?

wagmare
3rd January 2009, 10:58
no but even "in singal fin\n" is not printing ... its not entering the slot readFromStdout2()
.. only after closing the gtk window then only i get the output ....

wysota
3rd January 2009, 16:46
Maybe the other process doesn't write anything on its standard output? Or it doesn't flush the descriptor...

wagmare
5th January 2009, 05:53
thanks for reply ..:D but i use merged channels and i am getting standard output but only after the process ie.. the gtk window closed ..

wysota
5th January 2009, 09:25
That's why I'm saying the other process is not flushing its stdout descriptor. Your data gets stuck in a pipe buffer between the two processes. If you are the author of "the other app", simply write a newline after the data or call (f)flush() on stdout.

wagmare
6th January 2009, 05:37
That's why I'm saying the other process is not flushing its stdout descriptor. Your data gets stuck in a pipe buffer between the two processes. If you are the author of "the other app", simply write a after the data or call (f)flush() on stdout.

thanks for reply :) ..
i try the both ((f)flush() ,newline )but still its giving the same ... when the process gets finished and closed only its giving the stdout.
i cant get any thing .........

wysota
6th January 2009, 11:12
What if you run that application separately from the terminal? Do you see its output immediately?

wagmare
6th January 2009, 11:30
yes its running quickly ... after i execute gtk_demo separately it shows the output before showing the window ... its quick ..

wysota
6th January 2009, 12:55
Could we see some more code of yours?

wagmare
7th January 2009, 06:14
my code or that Gtk code ..?

my Qt code:


timer2 = new QTimer(this);
timer2->setSingleShot(TRUE);

timer_FS = new QTimer(this);
timer_FS->start(300);

container2 = new QX11EmbedContainer(this);
container2->setPalette(QPalette(Qt::red));
container2->setAutoFillBackground(true);


procc2 = new QProcess();
procc2->setProcessChannelMode(QProcess::MergedChannels);
connect(procc2, SIGNAL(readyReadStandardOutput()), this, SLOT(readFromStdout2()));




QHBoxLayout *layoout4 = new QHBoxLayout();
layoout4->addWidget(container2);

mainFrame5->setLayout(layoout4);

connect(timer2, SIGNAL(timeout()), this, SLOT(embed2()));
connect(FPSBackButton, SIGNAL(clicked()), this, SLOT(timeout_bg2()));
connect(timer_FS, SIGNAL(timeout()), this, SLOT(timeout_FS()));


/*setFPScanner =1 will be set when a embed Button is clicked in my Qt app*/


void
MainWindow::timeout_FS(){

if(setFPScanner == 1)
mainstackedWidget->setCurrentIndex(4);
else if(embedFPScanner == 1)
{
QString program4 = "./gtk_demo2";
//QString program4 = "./animate";
QStringList arguments2;
if(procc2->state() != QProcess::Running)
{
procc2->start(program4, arguments2);
}
}

}


void MainWindow::readFromStdout2()
{
int j = 4;
bool ok;
QString out;
out = procc2->readAllStandardOutput();
j = out.toInt(&ok, 16);
printf("j =%d\n",j);
if(j == 1)
printf("device not connected\n");
else{
printf("the device connected\n");
timer2->start(2000);
}

}




void MainWindow::embed2()
{

container2->embedClient(widedit->text().toInt(&ok, 16));
}


/*this will be set setFpscanner when a back button in my Qt app is clicked*/


void MainWindow::timeout_bg2()
{
timer_FS->stop();
setFPScanner = 0;
mainstackedWidget->setCurrentIndex(1);
timer_FS->start();
}


this is my whole code in that part i written .... if u find any error please help me ...:)

wysota
7th January 2009, 09:42
And how do you output the data from the slave app?

wagmare
7th January 2009, 11:12
And how do you output the data from the slave app?

the gtk window will print 1 using printf if the device is not connected else it print 0 .. externly running
gtk_demo.c



if(retval != FTR_RETCODE_OK){
if(retryCountLevel1 <= MAX_RETRY_COUNT){
FreeResource();
usleep(MAX_RETRY_SLEEP);
retryCountLevel1++;
goto retry_level1;
}else{
dump_line(stdin);
printf("1");
FreeResource();
return -1;
}
}
printf("0\n");

this is the gtk code ..
so i use
my Qt app
readyReadStandardOutput() for the process and i try to get that as


int j = 4;
bool ok;
const char *str;
QByteArray ba;
QString out;
out = procc2->readAllStandardOutput();
ba = out.toLatin1();
str = ba.data();
//j = out.toInt(&ok, 16);
printf("the string on returning:%s\n",str);
if(strcmp(str,"1") == 0){
printf("device not connected\n");
}
else{
printf("the device connected\n");
timer2->start(2000);
}

}


did u find any thing i done foolishly ...
if so please help ..