How does Qt5 redirect qDebug() statements to the Qt Creator 2.6 console

前端 未结 2 1484
傲寒
傲寒 2020-12-09 11:24

After searching around for a reason that qDebug() statements work fine with Qt\'s standard message handler but fail when I switch to my own, I\'m appealing here to see if an

2条回答
  •  星月不相逢
    2020-12-09 11:50

    I can't reproduce your issue: this works correctly for me.

    #include 
    #include 
    #include 
    
    #include 
    #include 
    
    void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
    {
        QByteArray localMsg = msg.toLocal8Bit();
        fprintf(stderr, "MESSAGE (%s:%u %s): %s\n", context.file, context.line, context.function, localMsg.constData());
        fflush(stderr);
    }
    
    int main(int argc, char **argv)
    {
        QCoreApplication app(argc, argv);
        qInstallMessageHandler(myMessageOutput);
        qDebug() << "Printed in the console";
        qInstallMessageHandler(0);
        qDebug() << "Also printed in the console";
        return app.exec();
    }
    

提交回复
热议问题