0

I am setting the environment for a program I will be launching using QProcess. One of the environment variables I need to set MAY contain spaces, slashes, and quotes. What is the right way to handle this?

I've tried escaping quotes, and adding a \" to either end of the string but my environment variable is always blank. Can someone share a code sample of how to handle this?

I'm thinking about this:

a = "\""+a.replace(QRegExp(QLatin1String("(\\\\*)\"")), QLatin1String("\\1\\1\\\""))+"\"";

but after numerous attempts I thought I would ask for help too.

For example, if a is:

A" \ / '

and I try to set an environment variable through

environment << "a=\""+a+"\""

and pass environment as the first parameters for a qprocess run, then the variable is NOT set when the qprocess starts, due to the fact that the environment ignores the malformed string.

TSG
  • 4,242
  • 9
  • 61
  • 121
  • Try to set an environment variable to [X" c' ] in your QProcessEnvironment then run a shell, and you will see the variable is NOT set. It has to be escaped. That is my question...how to escape it properly – TSG Mar 23 '14 at 16:16
  • Such things are very much platform dependent. What is the exact platform? (Windows version or Linux distribution, and exact Qt version and bitness). What you describe may be a bug or a platform limitation. – Kuba hasn't forgotten Monica Mar 23 '14 at 18:45
  • Running Qt 5.1.0 on RedHat 6, 64 bit. – TSG Mar 23 '14 at 19:35
  • `QProcessEnvironment` doesn't have an `operator<<`. It has an `insert` method. Your code doesn't agree with your narrative: in the title you claim use of `QProcessEnvironment`, yet the code looks like you use the old `setEnvironment` call that takes a string list. You should *not* be using the old `setEnvironment` method. Use `setProcessEnvironment` method instead. – Kuba hasn't forgotten Monica Mar 24 '14 at 05:25
  • I build a QStringList of environment variables and then insert them one at a time into the QProcessEnvrionment. I didn't say QProcessEnvironment has an operator<<. – TSG Mar 24 '14 at 13:36
  • What I'm saying is: don't do it the way you do! Use the `QProcessEnvironment` class and the `setProcessEnvironmentCall`. – Kuba hasn't forgotten Monica Mar 24 '14 at 14:20
  • I build a string list, and then add them one at a time to QProcessEnvironment. The first parameter of QProcess run is a QProcessEnvironment. But his is getting more off topic - please leave comments relating to the top – TSG Mar 24 '14 at 21:19

0 Answers0