Windows控制台和Qt Unicode文本

use*_*297 12 unicode console qt

我花了一整天的时间试图弄明白这一点,没有运气.我看着Everywhere,但工作代码没有运气.

操作系统:Win XP Sp2 IDE和FRAMEWORK:C++,Qt Creator 2.0.

我试图输出一些unicode(UTF-8)文本到Windows控制台,但我看到的只是乱码代替unicode字符.我知道win控制台确实支持unicode(自2000年以来)......至少根据维基百科和网上很多人的说法,我不知道如何让它与Qt一起工作.我见过的大多数"解决方案"(没有见过很多)使用C++和WInAPI技术......我不能使用它,因为这不是Qt方式.我正在使用QStrings和Qt!

代码如下.我拿出了所有不同的东西,我试图保持代码简单的帖子.希望有人可以让代码工作.

#include <QtCore/QCoreApplication>
#include <QString>
#include <QTextStream>          
#include <QDate>
#include <QFile>
using namespace std;

int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);

    QTextStream qin(stdin);         
    QTextStream qout(stdout);       

    //The last 2 chars in QString each need a double slash for an accent.
    QString szqLine = QString::fromUtf8("abc áéüóöú ??");

    //I want this text console output to be in red text color.
    qout << "Bellow are some unicode characters: " << endl; 

    //The Win XP console does not display the unicode chars correctly!!
    //The cosole does not display unicode chars even though it is capable
    //according to wikipedia.  I just don't know how with Qt.
    //I want this text output in white(or default font color, not red.)
    qout << szqLine << endl;

    //Would be nice to get some unicode input from console too.
    qout << "Write some unicode chars like above: " << endl;
    QString szqInput;
    szqInput = QString::fromUtf8(qin.readLine());
    qout << "You wrote: " << endl;
    qout << szqInput << endl;



    return app.exec();
}
Run Code Online (Sandbox Code Playgroud)

Ser*_*nov 9

好的,我用这段代码做了一些测试.无需特殊的控制台设置.

#include <QTextStream>

#ifdef Q_OS_WIN32
#include <windows.h>
#include <iostream>
#else
#include <locale.h>
#endif

class ConsoleTextStream: public QTextStream {
  public:
    ConsoleTextStream();
    ConsoleTextStream& operator<<(const QString &string);
};

ConsoleTextStream::ConsoleTextStream():
  QTextStream(stdout, QIODevice::WriteOnly)
{
}

ConsoleTextStream& ConsoleTextStream::operator<<(const QString &string)
{
#ifdef Q_OS_WIN32
  WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE),
      string.utf16(), string.size(), NULL, NULL);
#else
  QTextStream::operator<<(string);
#endif
  return *this;
}

class ConsoleInput: public QTextStream {
public:
  ConsoleInput();
  QString readLine();
};

ConsoleInput::ConsoleInput():
  QTextStream(stdin, QIODevice::ReadOnly)
{
}

QString ConsoleInput::readLine()
{
#ifdef Q_OS_WIN32
  const int bufsize = 512;
  wchar_t buf[bufsize];
  DWORD read;
  QString res;
  do {
    ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE),
        buf, bufsize, &read, NULL);
    res += QString::fromWCharArray(buf, read);
  } while (read > 0 && res[res.length() - 1] != '\n');
  // could just do res.truncate(res.length() - 2), but better be safe
  while (res.length() > 0 
         && (res[res.length() - 1] == '\r' || res[res.length() - 1] == '\n'))
    res.truncate(res.length() - 1);
  return res;
#else
  return QTextStream::readLine();
#endif
}

int main()
{
#ifndef Q_OS_WIN32
  setlocale(LC_ALL, "");
#endif
  ConsoleTextStream qout;
  qout << QString::fromUtf8("????? ?? ??????: ??? ??????? ?????? ????? ????? ?????\n");
  qout << QString::fromUtf8("????? ?? ????????: ????????????????????????????????????????????\n");
  qout << QString::fromUtf8("????? ?? ???????????: áéüóöú ??\n");
  qout << flush; // needed on Linux
  ConsoleInput qin;
  QString s = qin.readLine();
  qout << s << endl;
  s = qin.readLine(); // one more time, to ensure we read everything ok
  qout << s << endl;
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

在Windows上,它会打印除俄语和欧洲之外的所有文本的方框.看起来Lucida Console不支持希伯来语和日语.有趣的是,当我将文本从控制台复制到剪贴板然后粘贴到支持Unicode的地方(例如在浏览器中)时,它确实显示正确.这证明Windows实际上输出Unicode,只是不显示它.需要一些具有完全Unicode支持的控制台字体.

请注意,在上面的示例中,我只覆盖了一个operator<<(),但如果我想使用它们,我需要覆盖它们,因为它们返回QTextStream&但不是虚拟的,所以必须使它们全部返回ConsoleTextStream&,否则就像qout << 1 << someUnicodeString赢了工作正常.

我还在Linux上用UTF-8语言环境测试了这个例子,效果很好.

使用ReadConsoleW()的控制台输入是有效的,因为控制台默认配置为所谓的线路输入模式,所以它等待用户点击Enter但不等到有足够的字符可用来填充缓冲区,所以它确实完成了我们的工作want:如果缓冲区大小足够,则读取一行.