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:
@void myMainWindow::startTracking()
{
if (this->currTrackerExecPath.isNull())
{
QMessageBox::critical(this, tr("Select Tracker!"), tr("A tracker has not been selected, /nSelect a tracker..."), QMessageBox::Cancel, QMessageBox::Cancel);
return;
}this->trackingOn = true;
QStringList y, octy;
y<<this->currTrackerOutPath;
emit this->trackingTextUpdated(this->currTrackerExecPath);
emit this->trackingTextUpdated(this->currTrackerOutPath);QString z = "\n=================================\n";
z += "\n...beginning TRACKING...";QString x = this->currTrackerExecPath;
// Start the approp. process
int xx = this->trackMgr->getCurrentIndex() + 1;if (this->matlabTracker)
{
z += "\n...starting MATLAB exec [" + QString::number(xx, 10) + "] as a NEW PROCESS...";
emit this->trackingTextUpdated(z);
this->currTrackerExecPath = this->surfTrackerExecPath;
this->execList.at(xx-1)->start(this->currTrackerExecPath, y);
}
else
{
z += "\n...starting OCTAVE exec [" + QString::number(xx, 10) + "] as a NEW PROCESS...";
emit this->trackingTextUpdated(z);
octy = this->buildOctaveArgString();
this->currTrackerExecPath = this->octaveExecPath;/x << QString("-q");
x << QString("-p");
QString y(""");
y.append(this->octaveSourcePath);
y.append(QString("""));
x << y;
x << QString("--eval");
y.clear();
y.append(QString("""myprogram "));
//y.append(QString("myprogram "));
//y.clear();
y.append(this->currTrackerOutPath);
y.append(QString(""""));/this->execList.at(xx-1)->start(this->currTrackerExecPath, octy);
//this->execList.at(xx-1)->start("currTrackerExecPath -q -p """octaveSourcePath""" --eval """trackthiscar currTrackerOutPath"""");
//qDebug()<<octy.join(" ");
}
}QStringList myMainWindow::buildOctaveArgString()
{
QStringList x;/*
QString y("-q");
y.append(QString(" -p ""));
y.append(this->octaveSourcePath);
y.append(QString("" --eval "myprogram "));
y.append(this->currTrackerOutPath);
y.append(QString("""));
qDebug()<<"The Octave command string is -> "<<this->currTrackerExecPath<<y;
x << y;*/x << QString("-q");
x << QString("-p");
QString y(""");
y.append(this->octaveSourcePath);
//y.append(QString("C:\Tracker\octV273"));
y.append(QString("""));
x << y;
x << QString("--eval");
y.clear();
y.append(QString(""trackthiscar "));
//y.append(QString("trackthiscar "));
//y.clear();
//y.append(QString("C:\abc_45700_0_1.txt1"));
y.append(this->currTrackerOutPath);
y.append(QString("""));
x << y;
qDebug()<<"The octave command string is -> "<<this->currTrackerExecPath<<x.join(" ");return x;
}@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.
-
http://doc.qt.nokia.com/latest/qprocess.html#execute
bq. On Windows, arguments that contain spaces are wrapped in quotes.
So you don't add quotes yourself and it will work.
-
Maybe you didn't see this in my posting:
bq. 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.
-
[quote author="livingWithQt" date="1280938914"]Maybe you didn't see this in my posting:
bq. 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. [/quote]
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?
-
bq. I’m not master with Qt, so please be patient with my newbie’ish helping…
I apologize if my reply sounded rude or something.
bq. 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?
-
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.
-
Just tried with double quotes with the below snip, and even that works!
@
QString cmd("firefox");
QStringList args;
args << "http://www.google.co.in/search?q="qt+jambi"";
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" -
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 @code here @ ... and everything will be preserved :)
-
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...
bq. 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.
-
@
QStringList qslArguments;qslArguments.append("127.0.0.1"); qslArguments.append(qsStrapId); 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!