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

前端 未结 2 1476
傲寒
傲寒 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:46

    As Frank Osterfeld mentioned in his comment:

    On windows, qDebug() uses the debug channel, not stderr.

    After delving into the QDebug code and QMessageLogger I've found my answer. The handy WinAPI function OutputDebugString.

    Usage (Modified from peppe's):

    #include 
    #include 
    #include 
    
    #include 
    #include 
    #include 
    
    void MyMessageOutput(QtMsgType Type, const QMessageLogContext& Context, const QString &Message)
    {
        OutputDebugString(reinterpret_cast(Message.utf16()));
    }
    
    int main(int argc, char **argv)
    {
        // A GUI application
        QApplication app(argc, argv);
    
        // Custom handler
        qInstallMessageHandler(myMessageOutput);
        qDebug() << "Printed in the console using my message handler in a windows GUI application";
    
        // Default handler
        qInstallMessageHandler(0);
        qDebug() << "Also printed in the console!";
    
        // Show GUI here
        //MainForm *MF = new MainForm();
        //MF->show();
    
        return app.exec();
    }
    

提交回复
热议问题