August 4, 2010

livingWithQt livingWithQt
Lab Rat
2 posts

Problem using QProcess with arguments that have quotes & contain spaces [Win32/64]

 

Hi all

I’ve been trying to run an external program that takes multiple arguments from within my Qt application using QProcess, without success. Some of the arguments contain quotes and spaces. Here is the command that I want to execute:

[ P.S. The ‘forward slash’es I put in several places have been removed in the preview. I don’t know if they will be missing from the final post. ]

c:\Octave\3.2.4_gcc-4.4.0\bin\octave.exe -q -p “c:\MyProg\octV273” —eval “myprogram c:\project\a_b_c_d\2\frame_12345_2.txt1”

Arguments ‘3’ and ‘5’ have to be passed exactly as above to Octave. ‘myprogram’ stands for a Matlab source file. I’ve tested this on the command-line, so I know the string is correct. My source to handle this is given below. Some of the comments indicate options that I’ve tried but that did not work:

  1. void myMainWindow::startTracking()
  2. {
  3.  if (this->currTrackerExecPath.isNull())
  4.  {
  5.   QMessageBox::critical(this, tr("Select Tracker!"), tr("A tracker has not been selected, /nSelect a tracker..."), QMessageBox::Cancel, QMessageBox::Cancel);
  6.   return;
  7.  }
  8.  
  9.  this->trackingOn = true;
  10.  
  11.  QStringList y, octy;
  12.  
  13.  y<<this->currTrackerOutPath;
  14.  emit this->trackingTextUpdated(this->currTrackerExecPath);
  15.  emit this->trackingTextUpdated(this->currTrackerOutPath);
  16.  
  17.  QString z = "\n=================================\n";
  18.  z += "\n...beginning TRACKING...";
  19.  
  20.  QString x = this->currTrackerExecPath;
  21.  
  22.  // Start the approp. process
  23.  int xx = this->trackMgr->getCurrentIndex() + 1;
  24.  
  25.  if (this->matlabTracker)
  26.  {
  27.   z += "\n...starting MATLAB exec [" + QString::number(xx, 10) + "] as a NEW PROCESS...";
  28.   emit this->trackingTextUpdated(z);
  29.   this->currTrackerExecPath = this->surfTrackerExecPath;
  30.   this->execList.at(xx-1)->start(this->currTrackerExecPath, y);
  31.  }
  32.  else
  33.  {
  34.   z += "\n...starting OCTAVE exec [" + QString::number(xx, 10) + "] as a NEW PROCESS...";
  35.   emit this->trackingTextUpdated(z);
  36.   octy = this->buildOctaveArgString();
  37.   this->currTrackerExecPath = this->octaveExecPath;
  38.  
  39.   /*x << QString("-q");
  40.   x << QString("-p");
  41.    QString y("\"");
  42.    y.append(this->octaveSourcePath);
  43.    y.append(QString("\""));
  44.    x << y;
  45.    x << QString("--eval");
  46.    y.clear();
  47.    y.append(QString("\"\"myprogram "));
  48.    //y.append(QString("myprogram "));
  49.    //y.clear();
  50.    y.append(this->currTrackerOutPath);
  51.    y.append(QString("\"\""));*/
  52.  
  53.   this->execList.at(xx-1)->start(this->currTrackerExecPath, octy);
  54.   //this->execList.at(xx-1)->start("currTrackerExecPath -q -p \"\"\"octaveSourcePath\"\"\" --eval \"\"\"trackthiscar currTrackerOutPath\"\"\"");
  55.   //qDebug()<<octy.join(" ");
  56.  }
  57. }
  58.  
  59. QStringList  myMainWindow::buildOctaveArgString()
  60. {
  61.  
  62.  /*
  63.  QString y("-q");
  64.  y.append(QString(" -p \""));
  65.  y.append(this->octaveSourcePath);
  66.  y.append(QString("\" --eval \"myprogram "));
  67.  y.append(this->currTrackerOutPath);
  68.  y.append(QString("\""));
  69.  qDebug()<<"The Octave command string is -> "<<this->currTrackerExecPath<<y;
  70.  x << y;*/
  71.  
  72.  x << QString("-q");
  73.  x << QString("-p");
  74.  QString y("\"");
  75.  y.append(this->octaveSourcePath);
  76.  //y.append(QString("C:\\Tracker\\octV273"));
  77.  y.append(QString("\""));
  78.  x << y;
  79.  x << QString("--eval");
  80.  y.clear();
  81.  y.append(QString("\"trackthiscar "));
  82.  //y.append(QString("trackthiscar "));
  83.  //y.clear();
  84.  //y.append(QString("C:\\abc_45700_0_1.txt1"));
  85.  y.append(this->currTrackerOutPath);
  86.  y.append(QString("\""));
  87.  x << y;
  88.  qDebug()<<"The octave command string is -> "<<this->currTrackerExecPath<<x.join(" ");
  89.  
  90.  return x;
  91. }

The code within “if (this->matlabTracker)” works fine, so I know this is a problem with the way I’m handling the string for Octave, in the ‘else’. In the QProcess documentation, I read that arguments with spaces are wrapped in quotes, so I tried not including the quotes – didn’t work. The doc. for the 2nd version of the ‘QProcess::start()’ method, which doesn’t have a separate QStringList for arguments, mentions that quotes ‘need to be both escaped and quoted’, with \” becoming \”\”\”. Tried this too, no luck.

Can anyone tell me what I’m missing/doing wrong here? Any help in resolving this argument problem with quotes & spaces would be greatly appreciated. I use Qt 4.6.2 with Eclipse Galileo under Windows 7.

13 replies

August 4, 2010

Smar Smar
Lab Rat
269 posts

http://doc.qt.nokia.com/latest/qprocess.html#execute

On Windows, arguments that contain spaces are wrapped in quotes.

So you don’t add quotes yourself and it will work.

August 4, 2010

livingWithQt livingWithQt
Lab Rat
2 posts

Maybe you didn’t see this in my posting:

In the QProcess documentation, I read that arguments with spaces are wrapped in quotes, so I tried not including the quotes – didn’t work. The doc. for the 2nd version of the ‘QProcess::start()’ method, which doesn’t have a separate QStringList for arguments, mentions that quotes ‘need to be both escaped and quoted’, with \” becoming \”\”\”. Tried this too, no luck.

August 4, 2010

Smar Smar
Lab Rat
269 posts

livingWithQt wrote:
Maybe you didn't see this in my posting:

In the QProcess documentation, I read that arguments with spaces are wrapped in quotes, so I tried not including the quotes – didn’t work. The doc. for the 2nd version of the ‘QProcess::start()’ method, which doesn’t have a separate QStringList for arguments, mentions that quotes ‘need to be both escaped and quoted’, with \” becoming \”\”\”. Tried this too, no luck.

I’m not master with Qt, so please be patient with my newbie’ish helping… I said that because I’ve recently had similar problems with Windows and after all I were able to somehow get it work after messing up with arguments until they started to work…

What kind of errors you get? Some kind of simple test about this would help me, but can you get arguments you are passing to octave out as seen by octave?

August 4, 2010

livingWithQt livingWithQt
Lab Rat
2 posts

I’m not master with Qt, so please be patient with my newbie’ish helping…

I apologize if my reply sounded rude or something.

but can you get arguments you are passing to octave out as seen by octave?

Are you talking about taking a look at the string I’m finally passing to the Octave executable? Yes, I already looked into this, but there’s no way in Qt (that I know of, I’m a newbie too… :)) to see what the QProcess object is actually doing with the 2 args (QString – exec. path; QStringList – arguments). Do you know of a way to do this?

August 4, 2010

Smar Smar
Lab Rat
269 posts

No problems, I just have too sharp tongue sometimes…

I don’t know what this “octave” is and how it expects to get the parameters herself, so I’d suggest to make a simple test program which you can use to test how the arguments behaves. If it works as expected with this dummy program, it is octave that interprets the arguments wrong.

August 5, 2010

Deleted Member # 4a2 Deleted Member # 4a2
Ant Farmer
1481 posts

spaces are easily handled, I just tried this with a jpg file in “Program Files” directory of windows:

  1.     QString cmd("mspaint");
  2.     QStringList args;
  3.     args << "c:\\Program Files\\tanu.jpg";
  4.     pr->start(cmd, args);

August 5, 2010

Deleted Member # 4a2 Deleted Member # 4a2
Ant Farmer
1481 posts

Just tried with double quotes with the below snip, and even that works!

  1.     QString cmd("firefox");
  2.     QStringList args;
  3.     args << "http://www.google.co.in/search?q=\"qt+jambi\"";
  4.     pr->start(cmd, args);

In my firefox, I had google with qt jambi search done and URL in the browser reads:
http://www.google.co.in/search?q=“qt+jambi”

August 5, 2010

Deleted Member # 4a2 Deleted Member # 4a2
Ant Farmer
1481 posts

to your comment:

>> [ P.S. The ‘forward slash’es I put in several places have been removed in the preview. I don’t know if they will be missing from the final post. ]

You can actually preview and see how it looks in the final post … or use

  1. code here
… and everything will be preserved :)

August 5, 2010

Deleted Member # 4a2 Deleted Member # 4a2
Ant Farmer
1481 posts

sorry .. use the @ symbol before and after ur code

August 5, 2010

Smar Smar
Lab Rat
269 posts

Offtopic, but… You can also edit your posts, instead of making double posts.

And about code tags and quote tags and others: there are buttons for them at panel at top of this textarea…

Just tried with double quotes with the below snip, and even that works!

Naturally, as it takes a QStringList as argument, it can handle all escaping by itself. It would be different story if you’d need to use single string for arguments and program; then you’d need to do the escaping yourself, because the framework can’t read your thoughts.

When thinking this, always using QStringList for arguments saves time and avoid idiomatic bugs… When you forget one or two quotes from somewhere.

August 5, 2010

Deleted Member # 4a2 Deleted Member # 4a2
Ant Farmer
1481 posts

Thanks Smar, new to the forum and didn’t realize I could edit a post :)

October 4, 2011

Jeroentje@home Jeroentje@ho..
Robot Herder
915 posts

  1.     QStringList qslArguments;
  2.  
  3.     qslArguments.append("127.0.0.1");
  4.     qslArguments.append(qsStrapId);
  5.  
  6.     qpProcess.start("BMI_REMOTE", qslArguments);

This just works fine. Don’t bother about quotes etc. Qt will do it for you if I’m not mistaken, dependent on the OS you are running. Windows normally has a / as a argument separator, Linux uses a space. Qt will fix it for you. It is also possible to handle it yourself using the overloaded function of process::start (). Here you can give just 1 Qstring with the program name and all parameters added yourself. You can easily debug it yourself, but that would make it a bit less OS independent.
good luck!

 Signature 

Greetz, Jeroen

October 4, 2011

Volker Volker
Ant Farmer
5428 posts

Please don’t necropost to threads that ended over a year ago. Thanks.

 
  ‹‹ [Moved] Qt Installation Guide for Windows      [SOLVED] OpenGL lists does not work in custom QGraphicsItem’s paint() with plain QGLWidget ››

You must log in to post a reply. Not a member yet? Register here!