Results 1 to 9 of 9

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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

  2. #2
    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

  3. #3
    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.

  4. #4
    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

  5. #5
    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

  6. #6
    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?

  7. #7
    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.

  8. #8
    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
  •  
Qt is a trademark of The Qt Company.