1

Im trying to use a mapping from language names into local filenames, for example:

QMap<QString, QString> map;
map.insert("Русский", "russian");

Yet when I dump out the values, it comes out null:

QMap<QString, QString>::const_iterator it = map.begin();
while (it != map.end())
{
    qDebug() << "Language: " << it.key();
    ++it;
}

Language: ""

Ive tried to set the QTextCodec as follows:

QTextCodec * codec = QTextCodec::codecForName("UTF-8");
QTextCodec::setCodecForTr(codec);
m_langMap.insert(codec->fromUnicode("Русский"), "russian");

Yet same results. What am I doing wrong here? I really need this map to use in several places without replicating code.

Thanks

== EDIT ==

Sorry, thought I was editing my own. Here's non-working code, about identical to the short sample I made that works. The work-file is a much much larger file.

in CTOR:

m_ui->setupUi(this);

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");

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

== EDIT 2 ===

I created a new question here:

Unicode characters in qt app dont show up

Community
  • 1
  • 1
Tim
  • 647
  • 1
  • 10
  • 21
  • Don't forget to save source file as UTF-8 or in 1251 code page. This works for me, if I save source file with 1251 encoding (Windows Cyrillic). – Nemanja Boric Feb 04 '13 at 21:23

1 Answers1

1

This works:

map.insert(QString::fromWCharArray(L"Русский"), "russian");

Edit:

Using QStringList works too:

QStringList sl;
sl << QString::fromWCharArray(L"Русский")
   << QString::fromWCharArray(L"English")
   << QString::fromWCharArray(L"日本語");
ui->comboBox->addItems(sl);
  • Thanks Roku, what does the L do there? And is there a way to print it back out using qDebug()? Thanks for your help! – Tim Feb 04 '13 at 21:28
  • You need to set codepage for your command prompt with `chcp` command. – Nemanja Boric Feb 04 '13 at 21:37
  • 1
    Also http://stackoverflow.com/questions/1810343/is-a-wide-character-string-literal-starting-with-l-like-lhello-world-guarantee – Nemanja Boric Feb 04 '13 at 21:38
  • Ok Im still not finding the answer on this. Im using a Linux machine. After I insert it into a map I need to add it to a Combo box and thats not working (Its not coming out as expected). How do I do this? – Tim Feb 05 '13 at 16:27
  • This works in my Linux machine: `ui->comboBox->addItem(QString::fromWCharArray(L"Русский"));` Is your source code file saved using UTF-8 encoding? –  Feb 05 '13 at 18:43
  • yes. Very odd. I setup another single small qt project to test this and its definitely UTF-8. My UTF-8 BOM is 'keep if already present', same results, I get blank entries. – Tim Feb 05 '13 at 19:14
  • ah nevermind, I was trying to do a QStringList inserting those. Turns out inserting them directly works. Is there a way to use a QStringList to iterate over them? – Tim Feb 05 '13 at 19:19
  • BOM shouldn't matter in the UTF-8 encoding. QStringList works fine for me, I edited the answer for that. Maybe you should show your nonworking code. –  Feb 05 '13 at 19:52
  • It would be better to add the additional information to your question than to my answer. –  Feb 05 '13 at 20:50