PDA

View Full Version : QMessageBox encoding problem



Yaoming
1st February 2014, 10:16
Hello.
I've got a problem with my QMessageBox, special characters like "É" or "ç" are replaced by a "?" with a black background. I've read numerous posts in vain. I've tried that:

QTextCodec::setCodecForLocale(QTextCodec::codecFor Name("ISO 8859-1")); in my main.cpp file. I've also tried to change encoding in the Advanced Save Options but it's not working.
What bothers me is that special characters are displayed correctly in the rest of my application (menus etc).
Any ideas about how I could solve this problem?
Thanks for your replies,
Cheers.

ChrisW67
2nd February 2014, 22:08
If your text file is saved with UTF-8 encoding and your compiler does not do something silly with "non-ASCII" characters then this:


QString thingy = QString::fromUtf8("Où sont mes accents?"); // French

should work. If the text file is saved with the ISO8859-1 Western European encoding then:


QString thingy = QString::fromLatin1("¿Dónde están mis acentos?"); // Spanish

In the worst case you might have to manually encode the characters:


// Portguese: Onde estão meus acentos?
QString thingo = QString::fromUtf8("Onde est\u00E3o meus acentos?"); // works with gcc but not IIRC Microsoft compilers

QString thingy = QString::fromUtf8("Onde est\xC3\xA3o meus acentos?"); // should work with either

QString foo = QString::fromUtf8("Nerede benim aksan vard\u0131r?"); // Turkish (dotless i)
QString bar = QString::fromUtf8("Nerede benim aksan vard\xC4\xB1r?");

ISO8859-1 (http://www.wikipedia.org/wiki/ISO/IEC_8859-1) does not completely support languages like Turkish. I used http://people.w3.org/rishida/tools/conversion/ to encode.

If they are fixed messages or control labels then you could enter them in English and use the translation mechanism to provide translated versions.

Without knowing what platform, compiler, languages, or code is involved it is hard to be more specific.

BTW: Apologies to native French, Spanish, Portuguese, and Turkish speakers for the Google Translate examples

Yaoming
4th February 2014, 14:02
Thanks for your reply.
However, my QMessageBox text isn't in a text file. I'm writing it when I create my QMessageBox.

Lesiok
4th February 2014, 15:23
How about QTextCodec::codecForCStrings and QTextCodec::codecForTr ?

ChrisW67
4th February 2014, 20:15
However, my QMessageBox text isn't in a text file. I'm writing it when I create my QMessageBox.
Yes. None of the options I presented have anything to do with a text file other than the one your source code is in.

Yaoming
6th February 2014, 08:15
Sorry I misunderstood your reply.
When I put QTextCodec, it only gives me access to setCodecForLocale. I can't see CodecforTr or codecforCstrings. I've included qtextcodec.h though.

Lesiok
6th February 2014, 16:58
Sorry, my mistake. Of course QTextCodec::setCodecForCStrings and QTextCodec::setCodecForTr

Yaoming
8th February 2014, 09:43
Yeah I tried that, I can't see those functions.
I've got QT5.2. I've included:

#include "qtextcodec.h" like that but I only have "setCodecForLocale and it's not correcting my special characters problem.

Lesiok
8th February 2014, 13:43
Yes, in Qt 5.2 API for codec is other than in 4.8.
By the way if You set codecForLocale to ISO 8859-1 the text in the code should be coded in ISO 8859-1. Maybe show us some real code.

Yaoming
8th February 2014, 15:24
I've solved my problem.
Thank you all for your answers, they really heped me :)

Lesiok
9th February 2014, 06:55
This show off how you did it - share your knowledge with others.

rodrigoaznar75
31st March 2016, 22:02
Hi Yaoming,

How did you manage to solve your problem?

I think I have a similar issue using QMessageBox::warning, my question text contains "é" character, but when the application runs it presents "Ãi" instead.

I have tried:
- set locale and enconding to pt_Br and UTF-8
- passing my text string with tr and directly

Nothing seems to work.
Please anyone could help?

anda_skoa
31st March 2016, 22:31
Under the assumption of using Qt5, is your source code file encoded in UTF-8?

Cheers,
_