PDA

View Full Version : QProcess setEvironment



bagels4ever
22nd August 2012, 15:57
My Qt application, which calls ffmpeg, is working great on Linux.

It works on OS X as long as I launch it from a Terminal, but not if I launch it from the desktop; so obviously from the GUI launcher it's not inheriting the appropriate environment.

So I'm trying to utilize QProcess systemEnvironment

The Qt docs gives this Windows-ian example:


QProcess process;
QStringList env = QProcess::systemEnvironment();
env << "TMPDIR=C:\\MyApp\\temp"; // Add an environment variable
env.replaceInStrings(QRegExp("^PATH=(.*)", Qt::CaseInsensitive), "PATH=\\1;C:\\Bin");
process.setEnvironment(env);
process.start("myapp");

Which I assume I would adapt like this:



void startProcess()
{
QProcess process;
QStringList env = QProcess::systemEnvironment();
env << ":/usr/local/bin"; // Add the bin dir to PATH
process.setEnvironment(env);
process.start("????");

My three questions:
1. I am not sure if I am adding /usr/local/bin to PATH or not. I think I am.
2. What exactly am I satarting with process.start ? main? mainwindow.cpp ? what is it asking for here?
3. Also, am I right to be placing that block of code in main.cpp above int main or is there a better place for it.

Thanks in advance. Qt is amazing so far!

spirit
22nd August 2012, 16:53
My three questions:
1. I am not sure if I am adding /usr/local/bin to PATH or not. I think I am.


No, you are not. I don't see PATH variable in your code.



2. What exactly am I satarting with process.start ? main? mainwindow.cpp ? what is it asking for here?


You should run an app, eg app_name.app or app_name.app/Contents/MacOS/app_name.



3. Also, am I right to be placing that block of code in main.cpp above int main or is there a better place for it.

It's hard to say what you want.

Side question: Are you trying to set system variable to run your own app or for running other app?

bagels4ever
22nd August 2012, 20:12
OK, it's becoming a bit clearer now.

I believe I have the code in the correct place and acting, more or less, on the correct funciton.



QProcess commandProcess;
QStringList env = QProcess::systemEnvironment();
env << "PATH"; // Add an environment variable
env.replaceInStrings(QRegExp("^PATH=(.*)", Qt::CaseInsensitive), "PATH=$PATH:/usr/local/bin");
commandProcess.setEnvironment(env);
commandProcess.start("ffmpeg", args);


My only question now, I think, is whether I'm setting the PATH correctly. I'm having trouble using printf's to actually see what the variables are that I'm trying to change.

spirit
22nd August 2012, 20:20
I see.
You may also want to look at QProcessEnvironment.