Console output in a Qt GUI app?

前端 未结 17 1788
星月不相逢
星月不相逢 2020-11-28 07:06

I have a Qt GUI application running on Windows that allows command-line options to be passed and under some circumstances I want to output a message to the console and then

17条回答
  •  南笙
    南笙 (楼主)
    2020-11-28 07:49

    I used this header below for my projects. Hope it helps.

    #ifndef __DEBUG__H
    #define __DEBUG__H
    
    #include     
    
    static void myMessageOutput(bool debug, QtMsgType type, const QString & msg) {
    
        if (!debug) return;
    
        QDateTime dateTime = QDateTime::currentDateTime();
        QString dateString = dateTime.toString("yyyy.MM.dd hh:mm:ss:zzz");
    
        switch (type) {
    
            case QtDebugMsg:
                fprintf(stderr, "Debug: %s\n", msg.toAscii().data());
                break;
            case QtWarningMsg:
                fprintf(stderr, "Warning: %s\n", msg.toAscii().data());
                break;
            case QtCriticalMsg:
                fprintf(stderr, "Critical: %s\n", msg.toAscii().data());
                break;
            case QtFatalMsg:
                fprintf(stderr, "Fatal: %s\n", msg.toAscii().data());
                abort();
        }
    }
    
    #endif
    

    PS: you could add dateString to output if you want in future.

提交回复
热议问题