PDA

View Full Version : QProcess - quotes and spaces in parameters under Linux



mateuszzz88
3rd July 2011, 21:02
Hello,
I'm trying to use ImageMagick with QProcess under Linux. The arguments are quite complicated, the whole command needed is for example:

convert /tmp/temporaryPicture20.png -matte -virtual-pixel transparent -define distort:viewport=1105x3241+4572829+2684426 -distort Perspective '592,154 4572830,2684542 1408,54 4573934,2684426 654,2432 4572942,2687666 ' +repage /tmp/temporaryPicture20_out.png
Documentation suggests that such parameters may be tricky in Windows and Unix is piece of cake, but still I don't get how to use QProcess. What i tried was:
a) puttin the whole command in QString and passing it to QProcess::start(QString) (without separate arguments)
b)


QString command = "convert";
QStringList args;
args
<<"/tmp/mapin.png"
<<"-matte"
<<"-virtual-pixel"
<<"transparent"
<<"-define"
<<"distort:viewport=1105x3241+4572829+2684426"
<<"-distort"
<<"Perspective"
<<"'592,154 4572830,2684542 1408,54 4573934,2684426 654,2432 4572942,2687666'"
<<"+repage"
<<"/tmp/mapout.png";
iProcess->start(command,args);

and some variations of b) (escaping spaces "\ ", escaping single quotes "\'", using double quotes instead of single ones and some other. In all cases finished(int,status) signal is emited with arguments 0,1 (1 is bad, right?), the output image is produced but not distorted according to -distort Perspective '(...)' , so (probably) some arguments are passed correctly, but this one is ignored as faulty.

How do i pass it correctly?

SixDegrees
3rd July 2011, 21:20
The numeric argument to 'perspective' isn't properly quoted.

I'd suggest using the variant that takes a single string argument. Then, you can print out the argument and examine it for errors, or simply try pasting it onto the command line to see if it runs as expected.

Note, too, that you may not be able to assume a 'standard' environment when invoking processes this way. You may have to call setEnvironment to ensure that ImageMagick is able to find what it needs in order to run.

boudie
3rd July 2011, 21:25
Maybe you should split the numbers that are separated by spaces into separate args.

mateuszzz88
3rd July 2011, 22:00
The numeric argument to 'perspective' isn't properly quoted.

I'd suggest using the variant that takes a single string argument.

Sorry, that's true, but it's typo in forum only (wouldn't even compile, wolud it? )
I tried single string argument - marked as a) :-) Printed command works, QProcess doesn't.


Maybe you should split the numbers that are separated by spaces into separate args.
The numbers are all one argument wrapped by single quotes.

Added after 5 minutes:

Well, boudie's post made me thinking about some more radical experiments. Turns out that not only in windows (as I undersood the documentation) arguments with spaces are quoted. Proper way is:


args
<<"/tmp/mapin.png"
<<"-matte"
<<"-virtual-pixel"
<<"transparent"
<<"-define"
<<"distort:viewport=1105x3241+4572829+2684426"
<<"-distort"
<<"Perspective"
<<"592,154 4572830,2684542 1408,54 4573934,2684426 654,2432 4572942,2687666"
<<"+repage"
<<"/tmp/mapout.png";
iProcess->start("convert",args);

There's no additional quotes for numbers list even though quotes are necessary when issuing command by hand.
Thank you for your time, sorry for not thinking it out earlier.

abbaasi69
30th August 2020, 19:44
well done. I had the same problem with running the following command:
transformMesh -scale '(1, 2, 1)'
which I should use QProcess as follows:

myProc.start("transformMesh" , QStringList() << "-scale" << "(1,2,1)" ); // without single quotes