PDA

View Full Version : Trouble with QProcess and getting back standard output



taraj
7th June 2013, 02:42
Hi,

I have a script that returns a number that is the percentage of partition used, it looks like


#!/bin/bash
case $1 in
root_partition)
devname="/dev/sda1"
let p=`df -k $devname | grep -v ^File | awk '{printf ("%i",$3*100 / $2); }'`
echo $p
;;
default)
echo "Unknown command"
;;
esac
exit 0

I have a QProcess that looks like this

QStringList arguments;
QString program = "script.sh";
arguments.clear();
_proc = new QProcess(this);
qDebug("proc running");
arguments << " root_partition ";

if (_proc->state() == QProcess::NotRunning)
{
_proc->disconnect();
connect(_proc, SIGNAL(readyReadStandardOutput()), this, SLOT(outLog()));
_proc->start(program, arguments);
_proc->setReadChannel(QProcess::StandardOutput);

_proc->waitForFinished();
}
}

void MyClass::outLog()
{
qDebug("outLog");

QByteArray array = _proc->readAllStandardOutput();
}


My problem is that running the script by hand returns 87 - the % used on that partition. But running the script in my QT code does not work. If I replace the line

QString program = "script.sh";
with

QString program = "echo";

I get "root partition" returned - as expected. But why cannot I not anything back from running the script in Qt? The outLog function never gets called? Is echo not standard output?

I have done quite a bit of Googling but other people working solutions are not working for me? :(

Thank you very much for helping.
cheers,
Tara

Qt4 on Linux

ChrisW67
7th June 2013, 05:56
You are using a relative path to the script. Ensure the current working directory of your Qt program is where you think it is, that the script is there, and that the current working directory is in PATH. You could use a fully qualified path to the script or ensure the script is on the system PATH (like echo) which quite often does not include the current working directory.

Also check what QProcess::error() returns.

taraj
7th June 2013, 07:35
Thanks!

Changed

QString program = "script.sh";

to


QString program = QDir().absoluteFilePath("script.sh");

and all working now!