Debugging 用qInstallMsgHandler输出UTF-8

Debugging 用qInstallMsgHandler输出UTF-8,debugging,qt,utf-8,Debugging,Qt,Utf 8,我想让我的调试处理程序(与qInstallMsgHandler一起安装)处理UTF-8,但是它似乎只能定义为void myMessageOutput(QtMsgType,const char*msg)和const char*不处理UTF-8(一旦显示,它只是随机字符) 是否有某种方法将此函数定义为void myMessageOutput(QtMsgType,QString msg),或者其他方法使其工作 这是我当前的代码: void myMessageOutput(QtMsgType type,

我想让我的调试处理程序(与
qInstallMsgHandler
一起安装)处理UTF-8,但是它似乎只能定义为
void myMessageOutput(QtMsgType,const char*msg)
const char*
不处理UTF-8(一旦显示,它只是随机字符)

是否有某种方法将此函数定义为
void myMessageOutput(QtMsgType,QString msg)
,或者其他方法使其工作

这是我当前的代码:

void myMessageOutput(QtMsgType type, const char *msg) {
    QString message = "";

    QString test = QString::fromUtf8(msg);

    // If I break into the debugger here. both "test" and "msg" contain a question mark.

    switch (type) {

        case QtDebugMsg:
        message = QString("[Debug] %1").arg(msg);
        break;

        case QtWarningMsg:
        message = QString("[Warning] %1").arg(msg);
        break;

        case QtCriticalMsg:
        message = QString("[Critical] %1").arg(msg);
        break;

        case QtFatalMsg:
        message = QString("[Fatal] %1").arg(msg);
        abort();

    }

    Application::instance()->debugDialog()->displayMessage(message);
}



Application::Application(int argc, char *argv[]) : QApplication(argc, argv) {
    debugDialog_ = new DebugDialog();
    debugDialog_->show();

    qInstallMsgHandler(myMessageOutput);

    qDebug() << QString::fromUtf8("我");
}
void myMessageOutput(QtMsgType,const char*msg){
QString消息=”;
QString test=QString::fromUtf8(msg);
//如果我在这里进入调试器,“test”和“msg”都包含问号。
开关(类型){
案例QtDebugMsg:
message=QString(“[Debug]%1”).arg(msg);
打破
案例QtWarningMsg:
message=QString(“[Warning]%1”).arg(msg);
打破
案例QTSG:
message=QString(“[Critical]%1”).arg(msg);
打破
案例QTALMSG:
message=QString(“[Fatal]%1”).arg(msg);
中止();
}
Application::instance()->debugDialog()->displayMessage(消息);
}
Application::Application(int-argc,char*argv[]):QApplication(argc,argv){
debugDialog=新建debugDialog();
调试对话框->显示();
qInstallMsgHandler(myMessageOutput);

qDebug()尝试在UTF8中传递数据,并在函数中使用类似

QString::fromUTF8

它在输入时使用const char*。

问题是
操作符如果您在调试器中单步执行代码,您将发现QDebug和qt_消息首先从const char*构造一个QString,然后在此字符串上使用toLocal8Bit

我能想到的唯一方法是:使用您自己的编码(如“[E68891]”)或其他一些编码,如uu编码或base64编码,它们只使用ASCII字符并在消息处理程序中解码字符串

您还应该考虑使用版本qDebug(“%s”、“string”)来避免引号和附加空白(参见此)。 编辑:toLocal8Bit发生在QDebug的析构函数中,该析构函数在QDebug语句末尾被调用(QDebug.h第85行)。至少在Windows平台上,它调用toLatin1,从而误解字符串。您可以通过在程序开始时调用以下几行来防止这种情况:

QTextCodec *codec = QTextCodec::codecForName("UTF-8");
QTextCodec::setCodecForLocale(codec);
在某些平台上,UTF-8似乎是默认的文本编解码器
#include <QtCore/QCoreApplication>
#include <stdio.h>
#include <QDebug>

void myMessageOutput(QtMsgType type, const char *msg)
{
  fprintf(stderr, "Msg: %s\n", msg);
}

int main(int argc, char *argv[])
{
  qInstallMsgHandler(myMessageOutput);

  QCoreApplication a(argc, argv);

  qDebug() << QString::fromUtf8("我");    
}
#包括 #包括 void myMessageOutput(QtMsgType类型,const char*msg) { fprintf(stderr,“消息:%s\n”,消息); } int main(int argc,char*argv[]) { qInstallMsgHandler(myMessageOutput); qcorea应用程序(argc、argv);
qDebug()谢谢,我刚刚试过,但是没有用。当我在调试器中检查消息字符串时,它只显示一个问号。当我用QString::fromUTF8转换它时,它仍然是一个问号。我已经添加了我的代码。知道我可能做错了什么吗?尝试类似于
“\0xE6\0x88\0x91”
而不是
”我"
,如果可以的话,那么问题在于编码。我只是尝试了一下,但没有成功。在这种情况下,“msg”只包含一个空格。我不知道还有什么可以尝试。我很惊讶他们没有将msg作为一个QString来开始,来解决所有这些问题。嗯。然后尝试类似于
QTextCodec::setcodeforcstring的东西(QTextCodec::codeforname(“UTF-8”);
在QApplication声明后的主函数中…感谢您的所有建议,但不知何故它仍然不起作用。我刚刚尝试将编解码器设置为UTF-8,但消息仍然显示为“?”谢谢,我尝试使用QString::fromUtf8()传递字符串,但结果是一样的(仍然是“?”)。有什么方法可以查看msg包含的实际字节吗?我将更新我的示例,以包含来自UTF8的内容。您是否正在实例化
QApplication
main对象?您是否对应用程序的字符集设置进行了特殊处理?请尝试我添加的简单示例。它始终有效。如果没有,请告诉我是什么字节通过迭代消息处理程序内部的字符串,您可以从消息处理程序内部获得消息,例如
printf(“msg=\”);for(unsigned char*c=(unsigned char*)msg;*c!=0;c++)printf(\\x%02x),(int)*c);printf(\“;\n”);
。如果您试图
printf(3),您应该获得
msg=“\x22\xe6\x88\x91\x22\x20”
消息处理程序的输入您必须确保您的终端正确支持UTF-8,否则只会显示空格。请参阅我的答案,为什么这在您的平台上有效,而在Windows上无效。我一定是遗漏了什么。我使用的是相同的代码,它一直在输出“?“在消息框和控制台中。如果我签入调试器,它也是”?”(虽然正确编码的字符显示正确)。我的所有文件都是UTF-8格式的,应用程序可以显示中文字符,但消息处理程序仍然不能。我在Windows 7上,这有什么区别吗?(也许在Mac和Linux上正确支持UTF-8,但在Windows上不支持UTF-8?)!
#include <QString>
#include <QDebug>
#include <QMessageBox>
#include <QApplication>

void
myMessageOutput(QtMsgType type, const char *msg)
{
    QMessageBox::information(NULL, NULL, QString::fromUtf8(msg), QMessageBox::Ok);
}

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

    qInstallMsgHandler(myMessageOutput);
    qDebug() << QString::fromUtf8("我");

    return 0;
}
QTextCodec *codec = QTextCodec::codecForName("UTF-8");
QTextCodec::setCodecForLocale(codec);
#include <QtCore/QCoreApplication>
#include <stdio.h>
#include <QDebug>

void myMessageOutput(QtMsgType type, const char *msg)
{
  fprintf(stderr, "Msg: %s\n", msg);
}

int main(int argc, char *argv[])
{
  qInstallMsgHandler(myMessageOutput);

  QCoreApplication a(argc, argv);

  qDebug() << QString::fromUtf8("我");    
}