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.