之前我问过Qt translations in C++ project are rendered as question marks in cmd and powershell问题。问题的关键是 .\app -h
的帮助消息翻译为西里尔字母,呈现为 ?????????
.
从那时起,我发现https://github.com/RSATom/Qt/blob/master/qtbase/src/corelib/tools/qcommandlineparser.cpp ,Qt 消息在 Windows 上使用 qInfo("%ls", qUtf16Printable(message))
打印。
在 cmd.exe 或 poweshell.exe 中,当尝试以这种方式输出西里尔文消息时,我收到问号 ?????????
。
我尝试将 cmd.exe 和 powershell.exe 的字体更改为 Lucida Console
并执行 chcp 10000
(如此处建议 UTF-16 on cmd.exe ),但是这个没有帮助。
这是最小的可重现示例:
// > cl main.cpp /I C:\Qt\5.12.12\msvc2017_64\include
// > link main.obj /LIBPATH C:\Qt\5.12.12\msvc2017_64\lib\Qt5Core.lib
#include "QtCore/QtGlobal"
#include "QtCore/QString"
int main()
{
qInfo("%ls", qUtf16Printable("Привет"));
return 0;
}
// > main.exe
// ??????
我真的很感激任何帮助。这里有两个问题。第一:如何在Windows下使用QCommandLineParser
。第二:如果QCommandLineParser
(使用qInfo("%ls", qUtf16Printable(message))
)没问题并且不是错误,那么我怎样才能让cmd显示全部对。
最佳答案
我不会假装答案,但在 Windows 上,对于 Unicode 控制台输出,您应该执行类似以下操作:
#include <QDebug>
#include <iostream>
#include <string>
#include <fcntl.h>
#include <io.h>
#include <stdio.h>
void myMessageOutput( QtMsgType type, const QMessageLogContext & context, const QString & msg )
{
Q_UNUSED( context );
std::wcout << msg.toStdWString() << std::endl;
if( type == QtFatalMsg )
abort();
}
int main()
{
_setmode( _fileno( stdout ), _O_U16TEXT );
qInstallMessageHandler( myMessageOutput );
const std::wstring rs = L"Привет";
const auto s = QString::fromStdWString( rs );
std::wcout << s.toStdWString() << std::endl;
qDebug() << s;
qInfo() << s;
return 0;
}
关于c++ - Qt utf-16 输出在 Windows cmd 和 powershell 中显示问号——在 C++ 中具有最小可重现示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74068642/