qDebug() doesn't support unicode strings on Wi

2019-05-18 10:23发布

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.

3条回答
Root(大扎)
2楼-- · 2019-05-18 10:56

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.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-05-18 11:03

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

查看更多
地球回转人心会变
4楼-- · 2019-05-18 11:11

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.

查看更多
登录 后发表回答