gpt4 book ai didi

qt - 在实例化 QApplication 后 qDebug() 时,我丢失了 “unicodeness”

转载 作者:行者123 更新时间:2023-12-04 00:59:10 26 4
gpt4 key购买 nike

在实例化 QApplication 对象后,我失去了打印 unicode 字符的能力。

从以下代码并包含所有需要的库:

int main(int argc, char** argv)
{
qDebug() << "aeiou áéíóú";
QApplication app(argc, argv);
qDebug() << "aeiou áéíóú";
return 0;
}

我得到这个输出:
aeiou áéíóú
aeiou áéíóú

我该如何解决这种奇怪的行为?我需要能够打印 unicode 字符串(UTF-8 格式)。

最佳答案

2017 UPDATE: This answer from 2011 applies for Qt 4. In Qt 5, the text codecs were eliminated, and all source is expected to be UTF-8. See "Source code must be UTF-8 and QString wants it"



Qt解释时 char *成字符串,它使用文本编解码器。这是全局设置的,您可以为项目选择所需的内容:

https://doc.qt.io/qt-4.8/qtextcodec.html#setCodecForCStrings

请注意,Qt 的默认值是 Latin-1,它可能会在 QApplication 构造函数调用堆栈的某处建立该默认值。如果您在项目中全局使用 UTF-8,您可以尝试:
int main(int argc, char** argv)
{
qDebug() << "aeiou áéíóú";

QApplication app(argc, argv);
QTextCodec *codec = QTextCodec::codecForName("UTF-8");
QTextCodec::setCodecForCStrings(codec);

qDebug() << "aeiou áéíóú";
return 0;
}

看看这是否能解决您的问题。

关于qt - 在实例化 QApplication 后 qDebug() 时,我丢失了 “unicodeness”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7667407/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com