Qt-jambi-interest Archive, October 2006
Get standard output from a shell command
Message 1 in thread
Hi,
I would like to run a simple shell command from a QLineEdit and get the
result.
So, I simply call the following method with as argument the command that
I would like to execute :
public boolean RunCommand(String cmd) {
QProcess process = new QProcess( );
try {
process.start(cmd);
process.closeWriteChannel();
QByteArray qba=process.readAll();
QTextCodec codec = QTextCodec.codecForLocale();
QTextDecoder decoder = codec.makeDecoder();
String result = decoder.toUnicode(qba);
System.out.println("cmd=" + cmd + "
Output=" + result + " qba=" + qba);
}
catch (Exception e) {
System.out.println(e);
return false;
}
return true;
}
But the result is the following :
cmd=ls Output= qba=
('ls' is a linux shell command similar as 'dir' under dos).
Could you tell me what is my error ?
Thanks a lot.
Regards, Vincent
Message 2 in thread
Hi, Vincent.
Vincent Roberfroid wrote:
> Hi,
>
> I would like to run a simple shell command from a QLineEdit and get
> the result.
> So, I simply call the following method with as argument the command
> that I would like to execute :
QProcess starts a new process asynchronously, so at the point where you
are reading its output, the command has yet to finish executing. The
easy way around this is for your process to wait for the other process
to finish before continuing. As follows:
>
> public boolean RunCommand(String cmd) {
> QProcess process = new QProcess( );
> try {
> process.start(cmd);
> process.closeWriteChannel();
process.waitForFinished();
> QByteArray qba=process.readAll();
> QTextCodec codec = QTextCodec.codecForLocale();
> QTextDecoder decoder = codec.makeDecoder();
> String result = decoder.toUnicode(qba);
> System.out.println("cmd=" + cmd + "
> Output=" + result + " qba=" + qba);
> }
> catch (Exception e) {
> System.out.println(e);
> return false;
> }
> return true;
> }
>
Another, perhaps more efficient way (if your application is running a
UI) is to have the process alert you when it's finished executing.
Something like the following (I haven't actually compile-tested this, so
there may be some typos). Note that this won't work if your process
exits before the process you're listening to and that your class must
extend QObject or any of its subclasses.
public boolean RunCommand(String cmd) {
QProcess process = new QProcess();
process.start(cmd);
process.closeWriteChannel();
process.finished.connect(this, "printOut()");
}
public void printOut() {
QProcess process = (QProcess) signalSender();
QByteArray qba=process.readAll();
QTextCodec codec = QTextCodec.codecForLocale();
QTextDecoder decoder = codec.makeDecoder();
String result = decoder.toUnicode(qba);
System.out.println("Result = " + result + " qba=" + qba);
}
Message 3 in thread
On Tuesday 31 October 2006 15:52, Eskil A. Blomfeldt wrote:
> public void printOut() {
> QProcess process = (QProcess) signalSender();
> QByteArray qba=process.readAll();
> QTextCodec codec = QTextCodec.codecForLocale();
> QTextDecoder decoder = codec.makeDecoder();
> String result = decoder.toUnicode(qba);
> System.out.println("Result = " + result + " qba=" + qba);
> }
Sorry for nitpicking, but the text decoder is only necessary if you decode
text in chunks. A complete text, like the output of a command read with
readAll(), can be decoded with the text codec itself:
public void printOut() {
QProcess process = (QProcess) signalSender();
QByteArray qba=process.readAll();
QTextCodec codec = QTextCodec.codecForLocale();
String result = codec.toUnicode(qba);
System.out.println("Result = " + result + " qba=" + qba);
}
One line less to debug :-)
Matthias