qDebug() doesn't support unicode strings on Windows

扶醉桌前 提交于 2019-12-06 04:04:31

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.

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

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)

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!