PDA

View Full Version : Passing to a console application (managed via QProcess) UTF-8 encoded parameters



Conel
26th January 2007, 13:32
Hi guys,

I'm playing now with QProcess and external console applications. The console application is accepting image file name as a command line parameter. The problem is that image file names may be weird and contain Unicode symbols (umlauts, greek symbols) wich are not included into ASCII or Local 8 Bit encoding on my computer.

I use the following code for launching console utility:


QProcess process_exiflist;
QStringList exiflist_params;
exiflist_params << "/o";
exiflist_params << "a";
exiflist_params << fileName;
process_exiflist.start(utils_exiflist_exe, exiflist_params);

The problem occurs when QProcess passes file name to the console application. Qt libs performs conversion of QString into char* (which is normal) and this char* contains locally encoded file name. But local encoding does not contain some Unicode symbols and console application can not find the file specified

The ideal solution would be to make console application to understand UTF-8 encoded command line parameters. Let's suppose I have such an application. The question is: how to make QProcess to encode QString into UTF-8 rather that in Local encoding.

Thanks

Chicken Blood Machine
26th January 2007, 18:31
exiflist_params << QString::fromLocal8Bit(fileName);

Conel
26th January 2007, 22:22
Nop, this is not the case

fileName variable is a QString (not a ByteArray), i.e. fileName is Unicode (not a Local8Bit encoded)

The problem is that fileName QString is converted by QProcess to QByteArray (== char*)(as I suppose with using toLocal8Bit() method) and then passed it to the console application. But I'd like fileName to be converted to UTF-8

Chicken Blood Machine
26th January 2007, 22:35
Did you try QString::toUtf8() ?

Conel
28th January 2007, 11:00
I thought about


exiflist_params << fileName.toUtf8();

but this is wrong since fileName.toUtf8() gives QByteArray which will be used for creating QString (costructor QString::QString ( const QByteArray & ba )) which will be inserted into exiflist_params. This cunstructror constructs QString assuming that QByteArray contains ASCII encoded data. This I think just like


exiflist_params << QString::fromAscii(fileName.toUtf8());

It seems to be weird and does not work

Bitto
29th January 2007, 22:26
On Mac OS X, QProcess uses UTF-8 for command line arguments. On Windows and Unix, it calls QString::toLocal8Bit(). If your file system uses UTF-8, just make sure your applications run the UTF-8 locale too (a good idea, in general). So fex, export LANG=en_US.UTF-8, and then launch your app. QProcess will then use UTF-8 for the arguments.

frankiefrank
28th January 2015, 09:18
I'm having the same issue - trying to write a German umlaut (ü) to QProcess but it doesn't work.

toLatin1() gets me "?", toUtf8() and toLocal8Bit() get me random symbols.

I didn't manage to understand from this thread how to address this. I'm running on Windows 7. Is there a way to set QProcess to handle UTF-8?

yeye_olive
28th January 2015, 10:42
I do not think there is a way to change QProcess' behaviour, which unfortunately abstracts arguments as Unicode strings instead of plain NULL-terminated byte sequences. Here are two suggestions for working around the problem, in unspecified order of ugliness:

Call QTextCodec::​setCodecForLocale() to set an UTF-8 codec, so that when QProcess calls QString::toLocal8Bit() to serialize the arguments, UTF-8 will be used. Unfortunately, this means changing an application-wide setting to solve a local issue.
Preprocess the arguments before adding them to the QProcess' argument list: QString::fromLocal8Bit(fileName.toUtf8()). Something may get lost in translation.

frankiefrank
28th January 2015, 10:45
Thank you for your reply!

After some thinking I figured I really could skip the whole cmd.exe usage and do something else. But I hope this thread is helpful to other users.