QTextStream and wchar_t



  • QTextStream doesn't support wchar_t pointers. Such as
    @QTextSteam ts
    ts << L"Hello World";@

    I added a simple operator to overcome the issue :
    @
    QTextStream & operator << ( QTextStream & ts, const wchar_t * sss ) {
    ts << QString::fromWCharArray (sss);
    return ts;
    }
    @
    but I found it odd that QTextStream lacked support, and whenever I find something odd like that it's usually because I'm doing something wrong that requires me to need something that isn't built in. So, any reason why it's not apart of the QTextStream class?

    [Edit: Please use @ characters to wrap code; mlong]



  • When I remember correctly, wchar_t is not as portable as using QString (it may differ in size from compiler to compiler).
    If you want to pass hardcoded unicode to something, make your code file UTF-8 (obviously) and then use QString::fromUtf8.

    e.g.
    @
    qDebug() << "▛▀▀▀▜";
    prints: ▛▀▀▀▜
    qDebug() << QString::fromUtf8("▛▀▀▀▜");
    prints: "▛▀▀▀▜"
    @



  • @DerManu To make it even more portable, the C++ compilation process dictates that the files must be ANSI encoded, so almost no compilers support UTF-8 source files.

    If you need to have a string in UTF-8, instead add a resource file with a UTF8 file and pull it from there, just like when you internationalize code in Qt using tr() and Qt linguist.



  • Yes, I completely agree with you, Raul, and handle it this way myself (I use ascii approximations of what I want to say hardcoded and the UTF-8 is in ts/qm translation files, although I nevertheless encode my code files as UTF-8 – that's a religious thing).

    @gosuperninja: if translation files aren't overkill for your project, rather use them than hardcoding UTF-8. But if you still want to do it, rather use QString than wchar_t. (When in rome, do as the romans do.)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.