Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTextStream and wchar_t
Forum Updated to NodeBB v4.3 + New Features

QTextStream and wchar_t

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 2.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gosuperninja
    wrote on last edited by
    #1

    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]

    1 Reply Last reply
    0
    • D Offline
      D Offline
      DerManu
      wrote on last edited by
      #2

      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: "▛▀▀▀▜"
      @

      1 Reply Last reply
      0
      • R Offline
        R Offline
        raulgd
        wrote on last edited by
        #3

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

        Raul Guerrero
        http://jimi.mx

        1 Reply Last reply
        0
        • D Offline
          D Offline
          DerManu
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved