Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Qt default encoding to UTF-8

In my Qt application my source code files are encoded as UTF-8. For the following code...

QMessageBox::critical(this, "Nepoznata pogreška", "Dogodila se nepoznata pogreška! Želite li zatvoriti ovaj program ?", QMessageBox::Yes, QMessageBox::No);

...when I show that message box, the character "š" wouldn't be displayed as "š", but as something strange. This is because Qt converts all C-strings as if they are encoded using LATIN-1. To solve this I've been using:

QMessageBox::critical(this, QString::fromUtf8("Nepoznata pogreška"), QString::fromUtf8("Dogodila se nepoznata pogreška! Želite li zatvoriti ovaj program ?"), QMessageBox::Yes, QMessageBox::No);

Is there a way to get rid of all the calls to QString::fromUtf8()?

like image 506
xx77aBs Avatar asked Jan 02 '12 21:01

xx77aBs


2 Answers

Have you tried using QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"))?

like image 186
Samuel Harmer Avatar answered Oct 05 '22 17:10

Samuel Harmer


setCodecForCStrings() had been deprecated. Try instead,

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

It worked for me.

like image 20
Anupam Srivastava Avatar answered Oct 05 '22 16:10

Anupam Srivastava