PDA

View Full Version : Call script shell with Qt



zeeb100
25th February 2009, 15:39
Hi,
why this code doesn't work ???



QStringList arguments;
arguments << "-c" << "who|cut -d' ' -f1|sort -u";
QProcess *exec;
exec =new QProcess(this);
exec->start("/bin/sh", arguments);
ui->label->setText(exec->readLine(10));

jpn
25th February 2009, 16:38
QProcess works asynchronously. Please read QProcess docs. You might be interested in "Synchronous Process API".

zeeb100
25th February 2009, 18:01
ok i do ... but the real problem it is ...

for Run matlab in batch mode the command in the shell is


matlab -nojvm -nosplash -r MyCommand

where MyCommand is the name of m file ...
first question :
how to call this in my Qt app...???
if i do


arguments << "matlab -nojvm -nosplash -nodisplay -r addmatrix";
exec->start("/bin/bash", arguments);

don't work
and second question :
if i open the terminal can i send the comand from my qt app???
sorry for the trouble and thanks in advance

jpn
25th February 2009, 18:42
matlab -nojvm -nosplash -r MyCommand

These look like individual command line arguments to me.





arguments << "matlab -nojvm -nosplash -nodisplay -r addmatrix";
exec->start("/bin/bash", arguments);


Here you are not doing anything that would have to be interpret by a shell like you did in the first post.


if i open the terminal can i send the comand from my qt app???
Sorry, I don't understand. What do you mean?

zeeb100
25th February 2009, 19:00
before I have used


while(exec->waitForReadyRead())
ui->label->setText(exec->readLine(10));

and this works


Here you are not doing anything that would have to be interpret by a shell like you did in the first post.

What can i do ??



Sorry, I don't understand. What do you mean?

I manually launch shell and manually the start matlab in bash mode.
after start my Qt program and redirect the standartd input on the standard input of the shell as if the program wrote in shell to the place mine....CAN I DO THIS ???

faldzip
25th February 2009, 21:24
I think this:


matlab -nojvm -nosplash -r MyCommand

should look:


arguments << "-nojvm" << "-nosplash" << "-nodisplay" << "-r" << "addmatrix";
exec->start("matlab", arguments);




I manually launch shell and manually the start matlab in bash mode.
after start my Qt program and redirect the standartd input on the standard input of the shell as if the program wrote in shell to the place mine....CAN I DO THIS ???


As I understand, you want to pass the input from you Qt app to the matlab?
I would try starting matlab with a QProcess, and then writing and reading it's input/output, like sample code in assistant:


QProcess gzip;
gzip.start("gzip", QStringList() << "-c");
if (!gzip.waitForStarted())
return false;

gzip.write("Qt rocks!");
gzip.closeWriteChannel();

if (!gzip.waitForFinished())
return false;

QByteArray result = gzip.readAll();

zeeb100
26th February 2009, 09:17
thanks for help ... it work... the right code is:



QStringList argo, list;
QProcess *exec;
exec =new QProcess(this);
argo <<"/Applications/MATLAB_R2008a/bin/matlab"<<"-nojvm -nosplash -nodisplay -r 'addmatrix(2,7)'";
list <<"PATH=/sw/bin:/sw/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/usr/X11R6/bin";
exec->setEnvironment(list);

exec->start("/bin/bash", argo);


if there are a different mode more easy please write :)