[Qt-interest] QProcess on windows and the dirty work of quotes...

Gilberto Cuba Ricardo gilbert at hlg.rimed.cu
Thu Apr 22 02:12:37 CEST 2010


Thank you Jason for your answer, but I really don't understand what
do you tell me.

Jason H escribió:

> I think all you need is LOVE. Oh and,
> qslAppParams << "--params=\"<absoulte path with 
spaces>>/filename.any\""

I said in the last email, that if I put this:
...
qslAppParams << "--params=\"<absoulte path with spaces>/filename.any\""
...
it make:
      [qprocess_win.cpp - static QString qt_create_commandline(...)]
      ...
      QString tmp = qslParams.at(i);
      // in the case of \" already being in the string the \ must also be escaped
      tmp.replace( QLatin1String("\\\""), QLatin1String("\\\\\"") );
      // escape a single " because the arguments will be parsed
      tmp.replace( QLatin1String("\""), QLatin1String("\\\"") );
      if (tmp.isEmpty() || tmp.contains(QLatin1Char(' ')) || tmp.contains(QLatin1Char('\t'))) {
          // The argument must not end with a \ since this would be interpreted
          // as escaping the quote -- rather put the \ behind the quote: e.g.
          // rather use "foo"\ than "foo\"
          QString endQuote(QLatin1String("\""));
          int i = tmp.length();
          while (i>0 && tmp.at(i-1) == QLatin1Char('\\')) {
              --i;
              endQuote += QLatin1String("\\");
          }
          args += QLatin1String(" \"") + tmp.left(i) + endQuote;
      } else {
          args += QLatin1Char(' ') + tmp;
      }
      ...

where the result is:

" \"--params=\\\"<absoulte path with spaces>/filename.any\\\"\" "

that is equal to:

(" "--param=\"<absoulte path with spaces>/filename.any\" ")

and it's not the expected value of argument for pass to my program.

I hope you will understand my, or tell me where is my error. In other
hand, I change the source code of qprocess_p.h, qprocess.h/cpp and
qprocess_win.cpp; and now it's work good.

I tell you more, I think that it make two things wrong for this kind
of program parameters (--param="<directory with spaces>/filename.ext")

- The first one is that it assume that the complete parameter need the
quotes and then it puts the quotes at the begin and at the end of
string when it detects that string contains space, same like this:
"--param="<directory with spaces>/filename.ext""

- The second one is that it changes all quotes in string for a new
backslash and quote (")->(\"), that, I don't know for what, what is
your proposal whether the operating system don't interpret this well.

When I make a changes in the source code of Qt in the library
QProcess, I added a new boolean parameter to a method start() and
startDetached() with the proposal of select if you wish insert quotes
into argument string.

It's look like this

     void start(const QString &program,
          const QStringList &arguments,
          OpenMode mode = ReadWrite,
          bool parseArgsQuotes = true);

     static bool startDetached(const QString &program,
                 const QStringList &arguments,
                 const QString &workingDirectory,
                 qint64 *pid = 0,
                 bool parseArgsQuotes = true);

Then whether I pass one parameter like this, it's not make a change and
the program interpret well the parameters and work fine.

-- 
Cheers,
Lic. Gilberto Cuba Ricardo





More information about the Qt-interest-old mailing list