Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unicode characters in qt app dont show up

I'm trying to display different language strings in my qt app by inserting each language into a QMap<QString, QString> so it can be re-used in several places and put into different combo Boxes across the application. I do this by creating the QMap like so in the CTOR:

m_langMap.insert(QString::fromWCharArray(L"English"), "english");
m_langMap.insert(QString::fromWCharArray(L"Dansk"), "dansk");
m_langMap.insert(QString::fromWCharArray(L"Nederlands"), "dutch");
m_langMap.insert(QString::fromWCharArray(L"Čeština"), "czeck");
m_langMap.insert(QString::fromWCharArray(L"Slovenský"), "slovak");
m_langMap.insert(QString::fromWCharArray(L"Magyar"), "hungarian");
m_langMap.insert(QString::fromWCharArray(L"Român"), "romanian");
m_langMap.insert(QString::fromWCharArray(L"Latviešu"), "latvian");
m_langMap.insert(QString::fromWCharArray(L"Lietuvių"), "lithuanian");
m_langMap.insert(QString::fromWCharArray(L"Polski"), "polish");
m_langMap.insert(QString::fromWCharArray(L"Português"), "portuguese");
m_langMap.insert(QString::fromWCharArray(L"Español"), "spanish");
m_langMap.insert(QString::fromWCharArray(L"Français"), "french");
m_langMap.insert(QString::fromWCharArray(L"Italiano"), "italian");
m_langMap.insert(QString::fromWCharArray(L"Svenska"), "swedish");
m_langMap.insert(QString::fromWCharArray(L"Русский"), "russian");
m_langMap.insert(QString::fromWCharArray(L"Українська"), "ukranian");
m_langMap.insert(QString::fromWCharArray(L"Русский"), "russian");
m_langMap.insert(QString::fromWCharArray(L"中文"), "chinese");
m_langMap.insert(QString::fromWCharArray(L"日本語"), "japanese");

I then insert them into the combo box:

QMap<QString, QString>::const_iterator it = m_langMap.begin();
while (it != m_langMap.end())
{
    ui->comboBox->addItem(it.key());
    ++it;
}

When the app runs, I see the following:

enter image description here

However, if I create a separate .ui file and insert the map the same way, I see the following (even if I include this separate Dialog class into the same application), so clearly there is no font issue as far as the App not knowing how to render the different character sets....yet I cant figure out why the first one won't render the character sets?

Can someone tell me why the first doesn't work but the second does? I checked the Designer and its Locale is set to 'C, Default' in both ui files I've shown below. I can't seem to figure out what else is causing the difference for the first not to work, and the second does work within the same application.

Thanks for any help!

The other test Dialog:

enter image description here

like image 643
Tim Avatar asked Nov 13 '22 11:11

Tim


1 Answers

Your code is correct, but the problem is that your source file cannot contain Unicode characters - apparently it is using different coding.

Save file as UTF-8 and everything should work!

enter image description here

like image 73
Nemanja Boric Avatar answered Nov 15 '22 13:11

Nemanja Boric