0

I'm using Qt 5.3.0. I need to add some Japanese characters into my code. I already add BOM but I still have to use QString::fromLocal8Bit to make characters to correctly be displayed.

For example:
I have a pushButton and I call ui->pushButton->setText("浦"); then the button will have with weird marks instead of the character .

I have to use ui->pushButton->setText(QString::fromLocal8Bit("浦")); to make it display right.

Is there a way to avoid the call of fromLocal8Bit by setting all creation of QString to pass the function fromLocal8Bit by default? without changing Qt source code. Or any other good ideas?

Jerry YY Rain
  • 4,134
  • 7
  • 35
  • 52
user3819226
  • 461
  • 1
  • 5
  • 17
  • 1
    I see no harm in using `QString::fromLocal8Bit`, maybe you could write a fast-user function as `QString MainWindow::str(QString s);` and use `setText(str("浦"));` – Protomen Jul 10 '14 at 06:49
  • Have you tried `->setText( L"浦" );` ? The `L` prefix tells the compiler it's a UTF-16 string (that's a Windows thing, not Qt). So this avoids conversions. – MSalters Jul 10 '14 at 09:54

1 Answers1

1

Since qt5 you should be able to put japanese character as string literal into source code if the source files are saved in UTF-8 encoding. (I tried by copy/pasting the character, with qt5.3.1 and Mingw4.8.2 compiler, and I see the correct character in my button).
If you are loading the character from a file be sure to read it using a QTextStream with it's encoding set to "UTF-16" (if the file is encoded in UTF-16).

KazukiCP
  • 127
  • 2