PDA

View Full Version : System Command QProcess



jd
10th February 2009, 14:52
Hello folks,

I'll execute a system command on a Linux maschine. I've created this in a QProcess, but the result was very small.



QString cmd("dpkg");

QProcess* process = new QProcess(this);
process->start(QFile::encodeName(cmd).data(), QStringList() << "-l" << "| grep" << "xorg");
process->waitForFinished();

QString tmp = process->readAll();
qDebug() << tmp;


The return has a length from one line. This is wrong if I type dpgk -l | grep xorg I becomes over 20 Lines.

If I try this:


process->start(QFile::encodeName(cmd).data(), QStringList() << "-l");


So, with one argument all works fine, if I had more as one argument, the result ist not correctly.


Can someone tell me how to fix the argument list?

Thanks.

so long
jd

// Sorry wrong forum, can someone move my post.

jpn
10th February 2009, 15:03
That's not a single command. You need to launch a shell to interpret the commands. See for example this post (http://www.qtcentre.org/forum/p-qprocess-and-spumux-post57472/postcount2.html).

jd
10th February 2009, 15:24
Hello,

I've modified my function, but it doesn't work. I've no result...



QStringList arguments;
arguments << "-c" << "dpkg" << "-l" << "|" << "grep" << "xorg";
QProcess* process = new QProcess(this);
process->start("/bin/sh", arguments);
process->waitForFinished();

QString tmp = process->readAll();
qDebug() << tmp;



I've executed the command in the shell manually and this works fine:


/bin/sh -c "dpkg -l | grep xorg"


So, were is the problem in my code.

so long and thanks for help

jd

jpn
10th February 2009, 15:30
Try

arguments << "-c" << "dpkg -l | grep xorg";

jd
10th February 2009, 15:47
Thanks, works fine now.

talk2amulya
10th February 2009, 17:26
Hi jpn,

though the query is solved now, i would appreciate it if u tell me why it worked with combining last 5 arguments into one string argument and not otherwise

thanks a heap!

jpn
10th February 2009, 17:31
i would appreciate it if u tell me why it worked with combining last 5 arguments into one string argument and not otherwise
Because it's a single argument for /bin/sh. From command line you'd run it like this:
/bin/sh -c "dpkg -l | grep xorg"
not like this:
/bin/sh -c dpkg -l | grep xorg
Right? :)

talk2amulya
10th February 2009, 17:36
i m gonna hide under my bed..that was so stupid, i should've caught that..thanks jpn!