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. UTF-8 characters acting strangely in Windows (but not Linux)

UTF-8 characters acting strangely in Windows (but not Linux)

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 2 Posters 4.2k Views
  • 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.
  • eggbertxE Offline
    eggbertxE Offline
    eggbertx
    wrote on last edited by
    #1

    I'm trying to figure out why my file handling code works just fine in Linux but acts strangely in Windows. Both compile without errors or warnings but where the non-ASCII characters are displayed with qDebug() or written to a file in Windows I just get "????"

    QTextStream out(&fileHandle);
    QString string = QString::fromUtf8("おはよう。");
    out << string;
    

    Is there a better way to do this?

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      If you're using Visual Studio it doesn't, by default, treat your source code as encoded in utf-8. Unless there's a BOM it is interpreted using your current code page, which means that, by default, with Visual Studio the only "safe" character set is ASCII and extended utf-8 characters will come out garbage . If you want Visual Studio to treat your files as utf-8 you need to make it.

      1 Reply Last reply
      5
      • eggbertxE Offline
        eggbertxE Offline
        eggbertx
        wrote on last edited by
        #3

        @Chris-Kawa I'm using Qt Creator in both Windows and Linux.

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by Chris Kawa
          #4

          Qt Creator is an IDE. It doesn't matter. You could be using a Notepad and it's all the same. I'm talking about the compiler. Which Qt package have you installed?

          eggbertxE 1 Reply Last reply
          0
          • Chris KawaC Chris Kawa

            Qt Creator is an IDE. It doesn't matter. You could be using a Notepad and it's all the same. I'm talking about the compiler. Which Qt package have you installed?

            eggbertxE Offline
            eggbertxE Offline
            eggbertx
            wrote on last edited by
            #5

            @Chris-Kawa Well yes, I know that. I'm using what was provided by the Qt Creator installer.

            1 Reply Last reply
            0
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #6

              There are many installers. There's an online installer which lets you install Qt packages for many different compilers and there are offline packages for a single specific compiler.
              Please go to Tools -> Options ->Build & Run -> Kits tab, select te kit you're using and tell us what it says in the "Compiler" combo box.

              eggbertxE 1 Reply Last reply
              0
              • Chris KawaC Chris Kawa

                There are many installers. There's an online installer which lets you install Qt packages for many different compilers and there are offline packages for a single specific compiler.
                Please go to Tools -> Options ->Build & Run -> Kits tab, select te kit you're using and tell us what it says in the "Compiler" combo box.

                eggbertxE Offline
                eggbertxE Offline
                eggbertx
                wrote on last edited by
                #7

                @Chris-Kawa Microsoft Visual C++ Compiler 14.0 (amd64)

                1 Reply Last reply
                0
                • Chris KawaC Offline
                  Chris KawaC Offline
                  Chris Kawa
                  Lifetime Qt Champion
                  wrote on last edited by Chris Kawa
                  #8

                  Right, so, as I said, you need to make it understand utf-8 with a switch. Add QMAKE_CXXFLAGS += /utf-8 to your .pro file and re-run qmake (Build ->Run qmake).
                  After that the source should be treated as utf-8, but that's just the first part.
                  The second part is setting the codec on the stream. The default coded is locale and platform specific, so you just got lucky without that on Linux (because most distros are utf-8 centric these days). In any case, when you're dealing with non-ASCII characters you should always try to specify the codec explicitly. It will spare you the trouble. To set a codec call out.setCodec("UTF-8"); before you write anything to the stream.

                  eggbertxE 1 Reply Last reply
                  5
                  • Chris KawaC Chris Kawa

                    Right, so, as I said, you need to make it understand utf-8 with a switch. Add QMAKE_CXXFLAGS += /utf-8 to your .pro file and re-run qmake (Build ->Run qmake).
                    After that the source should be treated as utf-8, but that's just the first part.
                    The second part is setting the codec on the stream. The default coded is locale and platform specific, so you just got lucky without that on Linux (because most distros are utf-8 centric these days). In any case, when you're dealing with non-ASCII characters you should always try to specify the codec explicitly. It will spare you the trouble. To set a codec call out.setCodec("UTF-8"); before you write anything to the stream.

                    eggbertxE Offline
                    eggbertxE Offline
                    eggbertx
                    wrote on last edited by
                    #9

                    @Chris-Kawa I had already set QMAKE_CXXFLAGS += /utf-8 in my pro file and I ran qmake already. But using out.setCodec("UTF-8"); fixed the issue.

                    1 Reply Last reply
                    1

                    • Login

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