'QProcess does not work if the path in argument list has a whitespace

I want to execute following command using QProcess, that works in cmd already if executed:

C:\\myApplication\\tools\\dcmodify.exe -v -ie -gin -nb -ma (0010,0010)=TestedData "C:\Users\user.name\Documents\My Tools Data\Temp\Demo Phantom Skin\dicom\*"

The first argument, which gives the path of the executable is defined as a QString:

    QString srcToolPath = QDir::toNativeSeparators(QDir::cleanPath(qApp->applicationDirPath() + Constants::TOOLS_PATH + QDir::separator() + toQString("dcmodify.exe")));

The argument list and path, where the executable should be executed, is defined as a QStringList:

QString dstDicomPath = QDir::cleanPath(Utilities::getTempPath() + QDir::separator() + toQString("Anon_") + QDateTime::currentDateTime().toString(Constants::DETAILED_DATE_TIME_FORMAT)) + QDir::separator();

QStringList argumentList;
argumentList <<
toQString(" -v") <<
toQString(" -ie") <<
toQString(" -gin") <<
toQString(" -nb") <<
toQString(" -ma (0010,0010)=TestedData") << 
toQString(" \"") + QDir::toNativeSeparators(dstDicomPath) + toQString("*\"");

and the process is started:

QProcess anonymizerProcess;
anonymizerProcess.start(srcToolPath, argumentList);

Since the dstDicomPath contains some whitespaces, I added quotes around it. Although the command is executed, somehow I don't get the result like in cmd. What I am doing wrong with dstDicomPath string?



Solution 1:[1]

OK, there are multiple unknowns, for example what toQString() does in your code, but nevermind. Let's start with the assumption that this command is correct and would work if you called it from the command line:

C:\myApplication\tools\dcmodify.exe -v -ie -gin -nb -ma (0010,0010)=TestedData "C:\Users\user.name\Documents\My Tools Data\Temp\Demo Phantom Skin\dicom\*"

now lets have a look at what QProcess::splitCommand() returns (note that I escaped the backslashes and quotes in the command by prepending backslash):

QString cmd= "C:\\myApplication\\tools\\dcmodify.exe -v -ie -gin -nb -ma (0010,0010)=TestedData \"C:\\Users\\user.name\\Documents\\My Tools Data\\Temp\\Demo Phantom Skin\\dicom\\*\"";
qDebug() << QProcess::splitCommand(cmd);

It displays

("C:\\myApplication\\tools\\dcmodify.exe", "-v", "-ie", "-gin", "-nb", "-ma", "(0010,0010)=TestedData", "C:\\Users\\user.name\\Documents\\My Tools Data\\Temp\\Demo Phantom Skin\\dicom\\*")

... and this is exactly what you need to pass to the program and arguments for calling QProcess::start(). Hence:

auto program = QString("C:\\myApplication\\tools\\dcmodify.exe");
auto arguments = QStringList() << "-v" << "-ie" << "-gin" << "-nb" 
                               << "-ma" << "(0010,0010)=TestedData" 
                               << "C:\\Users\\user.name\\Documents\\My Tools Data\\Temp\\Demo Phantom Skin\\dicom\\*";
QProcess process;
process.start(program, arguments);

So the message here is that you do not need to try to fix the quotes about whitespaced strings yourself. Qt does it for you automatically. All you need is to correctly split the program and arguments so that Qt knows where to put quotes.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 HiFile the best file manager