3

I have a line edit that contains a file name with Unicode-characters and it displays correctly in the GUI, but when I print it with qDebug(), it shows the Unicode symbols as question marks.

For example, for "C:/Test/абв" this code will show only "C:/Test/???".

This line:

qDebug() << ui->lineEditFileName->text();

Would show:

enter image description here

This problem happens only on Windows (both XP and 7), on Linux it works fine. The version of Qt is 4.8.0.

sashoalm
  • 75,001
  • 122
  • 434
  • 781

4 Answers4

4

Also check this "Region and Language" -> "non-Unicode Programs" setting in Control Panel. It`s helped me to fix wrong symbols in debug console.

screen

XandrGuard
  • 141
  • 1
  • 3
3

It seems that the Unicode text is 'lost in translation', because Qt Creator uses QString::fromLocal8Bit() when reading the debug output from the process.

I found the answer from this thread:

I don’t know what qDebug uses to put strings onto the console on windows (I assume QString::toLocal8Bit). I know that Qt Creator uses QString::fromLocal8Bit(…) to read the text from the process. That works great everywhere… but unfortunately there is one OS out there that still insists on using codepages that completely break any attempt to display more than one kind of script at a type.

sashoalm
  • 75,001
  • 122
  • 434
  • 781
  • 3
    I.e. instead of doing the right thing internally (converting to/from UTF-8), or the native thing (which would be UTF-16 on Windows), Qt instead uses the local 8 bit encoding and breaks Unicode text? Can't blame Windows for that, there are two sane ways to do it and Qt picks neither. – MSalters Jul 19 '13 at 14:21
  • 1
    I agree, there was no reason to choose to/fromLocal8Bit when they control both ends and know they both support UTF8. – sashoalm Jul 19 '13 at 14:51
1

Where to find the setting @XandrGuard describes in Windows 10: Language -> Administrative Language Settings -> change system locale -> check Beta: use unicode utf-8. (requires a reboot) enter image description here

enter image description here

enter image description here

benk
  • 377
  • 1
  • 4
  • 13
-1

Linux uses Unicode for it's terminal, Windows - does not. You can find out, which code page is used by typing chcp in cmd. What you need is to convert your string, using this code page before outputting it:

QTextCodec *codec = QTextCodec::codecForName("CP866");
qDebug() << codec->fromUnicode(ui->lineEditFileName->text());

Or set it for all c-strings:

int main()
{
    ...
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("CP866"));
    ...
}

The second piece of code will make "CP866" the default codec for all strings in your program.

Amartel
  • 4,248
  • 2
  • 15
  • 21
  • `qDebug()` in my case outputs in Qt Creator's Application Output pane, not in cmd, because my application is not a console application. – sashoalm Jun 02 '13 at 16:46
  • `Application Output` uses `Utf-8`. Are you using different text codec? – Amartel Jun 02 '13 at 16:54
  • No, I don't use, this happens on a newly created project, where I just have a line edit and a button, and I do `qDebug() << ui->lineEdit->text();` in the button's slot. If the string in the line edit is Unicode, it is displayed as question marks in the Application Output. – sashoalm Jul 18 '13 at 19:13
  • Windows absolutely uses Unicode for its terminal. You may not have selected a terminal font with enough glyphs, though, but that same mistake is possible on Linux. Just consider: what's the point of `WriteConsoleW`? The same font problem could exist in Qt Creator. Which font is used for the output panel? – MSalters Jul 18 '13 at 23:09
  • So you mean that the font might be the problem? But I can't figure out what the font is for the panel, there is only one font setting, and it is for the text editor itself, changing it does not affect the Application Output pane. – sashoalm Jul 19 '13 at 07:53
  • I just tried pasting Unicode text into the Application Output pane (you can actually edit inside the pane), and it shows fine. Only when it is printed via `qDebug()` it becomes question marks. If the problem was with the font, I assume that pasted text/manually entered text would also show as question marks? – sashoalm Jul 19 '13 at 07:59
  • IMHO, qDebug is broken. Console output should use WriteConsoleW on Windows. Setting the font can be done by e.g. Qt Creator internally. Heck, even some installers ask to change the default console font to something useful. – rubenvb Jul 19 '13 at 12:23
  • @sashoalm: If the pane shows Unicode when entered, the font is OK. – MSalters Jul 19 '13 at 14:18