PDA

View Full Version : Qprocess for a prompted program



muzgash
10th May 2009, 14:19
I want to run an external program which is prompted (specifically gnuplot) and communicate with it writing on his prompt.
I don't use writeToStdin() function because I'm using Qt4:


process::process(QString progname)
{
progname="gnuplot";
program=new QProcess;
program->start(progname,QIODevice::ReadWrite);
qDebug()<<program->isWritable();
// program->execute(proc);
qDebug()<<"paso execute";

program->write("plot sin(x) pause -1");
qDebug()<<"paso plot";
}

I added the start function because it wasn't writable without it.

bvs
10th May 2009, 19:42
Before you write to the gnuplot process you should wait until the process has started. Otherwise, the write comes to early and is ignored. The code should look as follows:



program=new QProcess;
program->start(progname,QIODevice::ReadWrite);
if (!program.waitForStarted()) return;
program->write("plot sin(x); pause -1;");


Hope it helps,
Burkhard

muzgash
10th May 2009, 22:22
It doesn't work, I tried with the function and with no QProcess inheritance, now the class process inherits from QProcess and it doesn't work, but is the same sh*t.
The code looks like this:



process::process()
{

start("gnuplot",QIODevice::ReadWrite);

qDebug()<<isOpen()<<"is open";
qDebug()<<"pass on start";

// qDebug()<<waitForStarted()<<"wait4started";
// write("plot sin(x);pause -1;");
// qDebug()<<"pass on plot";
// execute("plot sin(x);pause -1");
// qDebug()<<readAll();
}

void process::prun()
{
if(!waitForStarted()) return;
write("plot sin(x); pause -1;");
qDebug()<<"pass on plot";

// QProcess *gnuplot = new QProcess;
//
// gnuplot->start("gnuplot",QIODevice::ReadWrite);
//
// qDebug()<<gnuplot->isOpen()<<"is open";
// qDebug()<<"pass on start";
//
// while(!gnuplot->waitForStarted()) return;
// qDebug()<<gnuplot->waitForStarted()<<"wait4started";
// gnuplot->write("plot sin(x);pause -1;");
// qDebug()<<"pass on plot";
//
// qDebug()<<gnuplot->readAll();

}and the output looks like this:

true is open
pass on start
pass on plot
QProcess: Destroyed while process is still running.

ChrisW67
11th May 2009, 00:17
The last message suggests that your QProcess object is either going out of scope after being allocated on the stack, or being deliberately deleted after being allocated on the heap but before it has finished cleanly. I'd start by looking at the conditions that might destroy the QProcess object.

brezel
13th June 2012, 20:18
for the search engines...i had the same problem trying to execute gnuplot and writing commands to the QProcess. turns out what i was missing was newlines at the end of each command ;)




commands << "set datafile commentschars \";\"\n"
<< "set terminal png\n"
<< QString("set output \"").append(outfile).append("\"\n")
<< "set multiplot layout 2,1\n"
...

gnuplot.start("gnuplot", QIODevice::ReadWrite);
foreach (QString cmd , commands) {
gnuplot.write(cmd.toUtf8());
}
gnuplot.waitForFinished();



this code worked for me :)