Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6190791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:39:08+00:00 2026-05-24T02:39:08+00:00

I would like to make my debug handler (installed with qInstallMsgHandler ) handles UTF-8,

  • 0

I would like to make my debug handler (installed with qInstallMsgHandler) handles UTF-8, however it seems it can only be defined as void myMessageOutput(QtMsgType type, const char *msg) and const char* doesn’t handle UTF-8 (once displayed, it’s just random characters).

Is there some way to define this function as void myMessageOutput(QtMsgType type, QString msg), or maybe some other way to make it work?

This is my current code:

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("我");
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T02:39:09+00:00Added an answer on May 24, 2026 at 2:39 am

    If you step through the code in the debugger you will find out that QDebug and qt_message first construct a QString from the const char* and then use toLocal8Bit on this string.

    The only way I can think of to circumvent this: Use your own coding (something like “[E68891]”) or some other coding like uu-encode or base64-encoding that uses only ASCII characters and decode the string in your message handler.

    You should also consider to use the version qDebug(“%s”, “string”) to avoid quotes and additional whitespace (see this question).

    Edit: the toLocal8Bit happens in the destructor of QDebug that is call at the end of a qDebug statement (qdebug.h line 85). At least on the Windows platform this calls toLatin1 thus misinterpreting the string. You can prevent this by calling the following lines at the start of your program:

    QTextCodec *codec = QTextCodec::codecForName("UTF-8");
    QTextCodec::setCodecForLocale(codec);
    

    On some platforms UTF-8 seems to be the default text codec.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to make a debug system in C++ where I can get
In C#, one can make a method such as this: [Conditional(DEBUG)] private void MyFunction()
I would like make an extension method for the generic class A which takes
Would like to make anapplication in Java that will not automatically parse parameters used
I would like to make a nightly cron job that fetches my stackoverflow page
I would like to make an ajax call to a different server (same domain
I would like to make a child class that has a method of the
I would like to make a list of remarkable robot simulation environments including advantages
I would like to make the update of the web application as automated as
I would like to make my application somewhat REST compliant. I am using Rails

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.