Results 1 to 9 of 9

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

  1. #1
    Join Date
    Apr 2006
    Location
    Minsk, Belarus
    Posts
    33
    Thanks
    2
    Thanked 6 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Passing to a console application (managed via QProcess) UTF-8 encoded parameters

    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:

    Qt Code:
    1. QProcess process_exiflist;
    2. QStringList exiflist_params;
    3. exiflist_params << "/o";
    4. exiflist_params << "a";
    5. exiflist_params << fileName;
    6. process_exiflist.start(utils_exiflist_exe, exiflist_params);
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Mountain View, CA
    Posts
    279
    Thanked 42 Times in 37 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Passing to a console application (managed via QProcess) UTF-8 encoded parameters

    Qt Code:
    1. exiflist_params << QString::fromLocal8Bit(fileName);
    To copy to clipboard, switch view to plain text mode 
    Save yourself some pain. Learn C++ before learning Qt.

  3. #3
    Join Date
    Apr 2006
    Location
    Minsk, Belarus
    Posts
    33
    Thanks
    2
    Thanked 6 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Passing to a console application (managed via QProcess) UTF-8 encoded parameters

    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

  4. #4
    Join Date
    Jan 2006
    Location
    Mountain View, CA
    Posts
    279
    Thanked 42 Times in 37 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Passing to a console application (managed via QProcess) UTF-8 encoded parameters

    Did you try QString::toUtf8() ?
    Save yourself some pain. Learn C++ before learning Qt.

  5. #5
    Join Date
    Apr 2006
    Location
    Minsk, Belarus
    Posts
    33
    Thanks
    2
    Thanked 6 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Passing to a console application (managed via QProcess) UTF-8 encoded parameters

    I thought about

    Qt Code:
    1. exiflist_params << fileName.toUtf8();
    To copy to clipboard, switch view to plain text mode 

    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

    Qt Code:
    1. exiflist_params << QString::fromAscii(fileName.toUtf8());
    To copy to clipboard, switch view to plain text mode 

    It seems to be weird and does not work

  6. #6
    Join Date
    Jan 2006
    Location
    Norway
    Posts
    124
    Thanked 38 Times in 30 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Passing to a console application (managed via QProcess) UTF-8 encoded parameters

    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.
    Bitto / Andreas Aardal Hanssen - andreas dot aardal dot hanssen at nokia
    Nokia Software Manager, Qt Development

  7. #7
    Join Date
    Dec 2010
    Location
    Israel
    Posts
    90
    Thanks
    59
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: Passing to a console application (managed via QProcess) UTF-8 encoded parameters

    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?

  8. #8
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Passing to a console application (managed via QProcess) UTF-8 encoded parameters

    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:
    1. 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.
    2. Preprocess the arguments before adding them to the QProcess' argument list: QString::fromLocal8Bit(fileName.toUtf8()). Something may get lost in translation.

  9. #9
    Join Date
    Dec 2010
    Location
    Israel
    Posts
    90
    Thanks
    59
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Passing to a console application (managed via QProcess) UTF-8 encoded parameters

    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.