PDA

View Full Version : QProcess - Working directory problem



jomarin
27th November 2008, 11:03
Hello,

I'm trying to run an external program using the QProcess class. As I got some problems, for the moment, it is just a small sh script displaying one environment variable and the working directory :



#!/bin/sh

pwd
echo $dir


Here is the code I use to run this script from my Qt program :



void S3geoa2Launcher::runS3geoa2()
{

QString program = "/usr2/home/jomarin/apps/testScript";

QString dirPath = "dir=";

dirPath.append("testUnix");

// create or modify the dir environment variable
putenv((char *)dirPath.toStdString().c_str());

QProcess *s3geoa2Process = new QProcess(this);

// Setting the working directory of the application
s3geoa2Process->setWorkingDirectory(QString(avantDistributeur->getPath().c_str()));

cout << "Launching " << program.toStdString() << endl;
cout << "Working directory "<< avantDistributeur->getPath() << endl;
cout << "Real working directory of the process "<< s3geoa2Process->workingDirectory().toStdString() << endl;

// Running the test script
s3geoa2Process->startDetached(program);

}


Finally, here is the output I get on the terminal :



Launching /usr2/home/jomarin/apps/testScript
Working directory /usr2/home/jomarin/testUnix/cadre/avd
Real working directory of the process /usr2/home/jomarin/testUnix/cadre/avd
/usr2/home/jomarin/svn_repo/alstomTools/trunk/debug
testUnix


The dir environement variable is correctly set, but the working directory of the program is wrong :(.

Am I doing something wrong ?

Thanks in advance,

Joel Marin.

high_flyer
27th November 2008, 11:47
The dir environement variable is correctly set
Are you sure?
from the code, the dir environment variable is:
"dir=testUnix"

yet all you other paths are not in '/' (root).
All the rest of the output matches what ever you gave in your code...

jomarin
27th November 2008, 12:56
Are you sure?
from the code, the dir environment variable is:
"dir=testUnix"


In fact, the test script shows that the dir environment variable is set to "testUnix". That is what I expected.



yet all you other paths are not in '/' (root).
All the rest of the output matches what ever you gave in your code...


What do you mean ? For me, I specified the working directory of the process as :
/usr2/home/jomarin/testUnix/cadre/avd
and when running, the pwd command gives :
/usr2/home/jomarin/svn_repo/alstomTools/trunk/debug

Joel.

high_flyer
27th November 2008, 15:51
I don't understand:
the output shows that the real working directory is the same one as you are setting.
Which variable is not as you expect?
You mean the pwd in your script?
That is because the pwd in your scripts puts out where it is in.
Your exe is either in /project_path/debug or /project_path/release depending on your configuration, so you need to set your working directory appropriately.

Btw - you don't need to convert from c_str() to QString() - just use getPath() like that:

s3geoa2Process->setWorkingDirectory(avantDistributeur->getPath());

It should not really change things though.

jomarin
27th November 2008, 16:26
Thanks for your answer, I resolved my problem using the QDir::setCurrent() function to set the working directory and it works.

Anyway, I still don't understand the behaviour of the QProcess::setWorkingDirectory() method.

From the documentation, I read :



void QProcess::setWorkingDirectory ( const QString & dir )

Sets the working directory to dir. QProcess will start the process in this directory. The default behavior is to start the process in the working directory of the calling process.


Thus, I don't understand why the pwd command of my script returns
/usr2/home/jomarin/svn_repo/alstomTools/trunk/debug
which is the working directory of my exe and not
/usr2/home/jomarin/testUnix/cadre/avd
which is the directory from where I want the new process to be started.

Could you clarify the use of the QProcess::setWorkingDirectory() method ?

Joel.