1

I'm writing a program in french. When I want to display french characters in QMessageBox, it was replaced by a "?" with a black background.I've tried that:

QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));

QMessageBox::information( this,  QString::fromUtf8("Fenêtre"), tr("Est-ce 
que vous voulez terminer cette simulation?"),tr("Oui"), tr("Non"), 0, 1 )   

Any ideas about how I could solve this problem? Thanks for your replies. I'm using Qt 5.1 on VS2010.

user0042
  • 7,917
  • 3
  • 24
  • 39
P.Don
  • 11
  • 3
  • You have to save the file in correct encoding. – Jarod42 Sep 20 '17 at 08:03
  • 1
    I suggest to use translation module of Qt: `QObject::tr` without non-ascii characters, and in ts files, you use correct translations (with non-ascii characters). – Jarod42 Sep 20 '17 at 08:05
  • 1
    This might be a font issue. All fonts are not made equal with respect to non-ASCII characters. – mouviciel Sep 20 '17 at 08:08
  • @Jarod42 Thanks, but it still doesn't work. It displays the same symbole. And I have verified that Qt Creator's default encoding is UTF-8, even I just use Qt Designer for my interface. – P.Don Sep 20 '17 at 08:28
  • @mouviciel So, It can't solve the problem because of the fonts? – P.Don Sep 20 '17 at 08:31
  • You should use only <127 ascii characters in your code. If you need non-english text - you should provide tr/ts files. See http://doc.qt.io/qt-5/internationalization.html – Dmitry Sazonov Sep 20 '17 at 08:54
  • I would suggest to split, and have `auto title = QString::fromUtf8("Fenêtre");` and check in debugger `title` content (You can also test `"Fen\xC3\xAAtre"`, `ê` is not utf-8) (so you can know from where there is an error). (but in reality, I would simply do `auto title = tr("Window");`) – Jarod42 Sep 20 '17 at 08:59
  • @P.Don: It seems you already use some, you have `tr("Oui")`. – Jarod42 Sep 20 '17 at 09:01
  • 1
    @P.Don: Please check for your code file which encoding is in the first combo from VS menu FILE->Advanced Save Options... – olya Sep 20 '17 at 09:43
  • @olya Thanks, you remind me of that, but I tried and it doesn't change... – P.Don Sep 20 '17 at 12:56
  • @Jarod42 Thanks for your help. First, I used directly `tr("windows")` but It still display the `windows` without translation, I don't know why. Second, when I debugger `title` by using `auto title = QString::fromUtf8("Fenêtre")`; it display `"Fen?tre"` . Third,test of `"Fen\xC3\xAAtre" ` can work right. But I'm confused how does it work...By the way, `utf-8` can not represent all of those characters? I'm a new...And Thanks again! – P.Don Sep 20 '17 at 13:20
  • You may try `QString::fromLatin1("Fenêtre")` (or `QString::fromUtf8(u8"Fenêtre")`). – Jarod42 Sep 20 '17 at 14:26
  • @Jarod42 THE ANSWER IS LATIN1!!!`QString::fromLatin1("Fenêtre")` – P.Don Sep 20 '17 at 14:53
  • @P.Don. `QString.fromUtf8()` requires **bytes** that are encoded as "utf-8". The text "Fenêtre" encoded as "utf-8" is `"Fen\xc3\xaatre"`. In C++ source code, a plain string-literal will be interpreted using the *local encoding* (even if the file is encoded as "utf-8"). If the local encoding is not "utf-8", any undecodable characters will be replaced by `?` in the return value of `QString.fromUtf8()`. So you should use `QString.fromLatin1()` instead. – ekhumoro Sep 20 '17 at 14:58
  • Then you could directly put your `QTextCodec::setCodecForLocale` as Latin1 and don't bother with `QString::fromLatin1`everywhere. Though if your file is really encoded in UTF-8 it does not make sense... – ymoreau Sep 20 '17 at 15:20
  • @ekhumoro By the way `setCodecForLocale(QTextCodec::codecForName("UTF-8")` can not change the **local encoding**? – P.Don Sep 21 '17 at 08:57
  • @P.Don. If `tr("windows")` doesn't work, it has nothing to do with encodings. There must be a problem with how you are using the translation files. I suggest you ask a new question about this, and give more details. As for `setCodecForLocale`: I think this will work if you use it with `QString::fromLocal8Bit()`. – ekhumoro Sep 21 '17 at 13:36
  • @P.Don. PS: you might want to take a look at [this question](https://stackoverflow.com/q/688760/984421), and experiment with some of the suggested solutions (not just those in the accepted answer). There are obviously complications with Windows and Visual Studio that go beyond the comments I made above. I can't test any of this myself, since I don't have access to a machine with that setup. – ekhumoro Sep 21 '17 at 14:04
  • @ekhumoro OK! You are so nice, I'll take a look of that question later, because I have something else to do rightnow. Thanks again! – P.Don Sep 22 '17 at 13:07

0 Answers0