1

In QML I have a TextArea. When I set the text property of this TextArea to "ÆØÅ" it shows "ÆØÅ" when the program runs.

I also get text through some functions that I want to show in the same TextArea, but then "ÆØÅ" is shown as "???".

The strings come from this C++ function:

QString Tts::morseToChar(QString morse)
{
    if(morse == ".-")               return "A";
    else if (morse == "-...")       return "B";
    ...
    ...
    else if (morse == ".-.-")       return "Æ";
    else if (morse == "---.")       return "Ø";
    else if (morse == ".--.-")      return "Å";
}

The return string gets added to a QML string:

property string ttsChar: ""
ttsChar += ttsSpeak.morseToChar(morse)
_ttsTextArea.text += ttsChar

All files are saved as UTF-8.

What conversion am I missing to get this to work?

uniquenamehere
  • 1,869
  • 3
  • 31
  • 60

2 Answers2

3

Try returning the universal character name instead of the utf-8 representation.

QString Tts::morseToChar(QString morse)
{
    if(morse == ".-")               return "A";
    else if (morse == "-...")       return "B";
    ...
    ...
    else if (morse == ".-.-")       return u8"\u00C6";
    else if (morse == "---.")       return u8"\u00D8";
    else if (morse == ".--.-")      return u8"\u00C5";
}

You can search for universal character names here which is where the names for Æ (\00C6), Ø (\00D8) and Å (\00C5) were found.

See this answer for more information on why the utf-8 representation isn't allowed in c++ source files.

fun4jimmy
  • 672
  • 4
  • 16
2

The default constructor for QStrings from char * uses the fromAscii() function.

Chances are your strings are actually encoded in UTF-8, so there's two things to try:-

a : Return wide chars:

return L"Æ";

b: Or, explicitly convert from UTF8

 return QString::fromUtf8("Æ");
Roddy
  • 66,617
  • 42
  • 165
  • 277
  • I tried returning wide char before asking, but got the error: C2664: 'QString::QString(QChar)' : cannot convert parameter 1 from 'const wchar_t [2]' to 'QChar' No constructor could take the source type, or constructor overload resolution was ambiguous. I tried your alternative b) but that did not do any difference as well. – uniquenamehere Dec 02 '13 at 15:34