PDA

View Full Version : Write Qt program to a language with special symbols



hakermania
5th September 2010, 22:23
I though that this count be done with the use of tr() but I was wrong...
I am trying to place this text to a button:
Déplacer à l'image &suivante
In french it means "Move to Next image". I place this text into the button with this way:
ui->nextButton->setText(tr("Déplacer à l'image &suivante"));
The Qt Creator, after compiling, show the button's text like this:
http://img828.imageshack.us/img828/4667/screenshotsn.png
As you can see the special character is shown wrong. Is any Qt can recognise these special characters? :confused::confused:

zeldaknight
6th September 2010, 01:34
I agree, this is odd behaviour ...

My suggestions would be to check the encoding of your source file - if it's ASCII the special characters might be conflicting with Qt's Unicode text system - make sure it's some variant of Unicode. That or look up the Unicode codes of é and à and use backslashes to put them in ...

If that doesn't work you might need to have a look at QTextCodec::setCodecForTr(). I'm not sure what codecs are available but there should be one which supports French letters.

I'm not 100% certain about this but hopefully one of the methods above will work.

Lykurg
6th September 2010, 07:47
Salut hakermania,

if you don't want to change the whole codec you can use QObject::trUtf8(). But you have to make sure, your file is encoded with UTF-8 too.

Lykurg

hakermania
6th September 2010, 14:43
Thanks for your suggestions. Lykurg, I used your suggestion and I finally found a solution!
I had to use button->setText(QString::fromUtf8("String here"));
and where there were special characters (like à ) I had to replace them with utf8 special from (e.g. é is written \u00e9)
There are several webpages where you can find these tables that specify the special characters for every special character.
So the action from this

ui->nextButton->setText(tr("Déplacer à l'image &suivante"));
should be done

ui->nextButton->setText(QString::fromUtf8("D\u00e9placer \u00e0 l'image &suivante")); :cool:

Lykurg
6th September 2010, 17:26
Try to use your special letters direct. You just have to make sure that your *.cpp file is encoded in utf8. So you will save time for escaping them.
ui->nextButton->setText(trUtf8("Déplacer à l'image &suivante"));

Bong.Da.City
6th September 2010, 18:12
Nice info, thak you all for this post

hakermania
6th September 2010, 21:10
No, i've tried this. I need to place these strange symbols....

MTK358
6th September 2010, 22:05
Aren't the contents of tr() supposed to be English?

Lykurg
7th September 2010, 00:03
Try the attached project without changing the file encoding and see if it works.
5154

Lykurg
7th September 2010, 00:05
Aren't the contents of tr() supposed to be English?
No, you can also perfectly write latin in tr(). The language just should fit in ASCII in normal case.