2012-06-25 85 views

回答

5

當您使用附加的調試器運行程序時,它將顯示在Visual Studio的輸出窗口中,但出於調試目的,我經常將調試輸出重定向到某種漂亮的日誌窗口,您可以通過使用功能qInstallMsgHandler

,我使用的代碼:

#include <qapplication.h> 
#include <qwidget.h> 
#include <qplaintextedit.h> 
#include <qmetaobject.h> 
#include <qthread.h> 
#include <qboxlayout.h> 
#include <qdatetime.h> 
#include <qdebug.h> 
#include <cstdio> 
#include <cassert> 


QWidget   *DEBUG_MESSAGE_DISPLAY_WIDGET = NULL; 
QPlainTextEdit *DEBUG_MESSAGE_DISPLAY_TEXTEDIT = NULL; 
void setupDebugDisplay(); 
void debugMessageDisplayFunc(QtMsgType type, const char *msg); 

int main(int argc, char* argv[]) 
{ 
    QApplication a(argc, argv); 
    a.setQuitOnLastWindowClosed(true); 
    setupDebugDisplay(); 
    // your code here.... e.g: 
    // YourMainWindow mainWindow; 
    int ret = a.exec(); 
    delete DEBUG_MESSAGE_DISPLAY_WIDGET; 
    return ret; 
} 

void setupDebugDisplay() 
{ 
    QWidget *widget = new QWidget(); 
    widget->setWindowTitle("Debug Log"); 
    widget->setAttribute(Qt::WA_QuitOnClose, false); //quit only when mainwindow is closed 
    QBoxLayout* layout = new QVBoxLayout(); 
    widget->setLayout(layout); 
    QPlainTextEdit *textEdit = new QPlainTextEdit(widget); 
    QFont font = QFont("Monospace"); 
    font.setStyleHint(QFont::TypeWriter); 
    textEdit->setFont(font); 
    textEdit->setReadOnly(true); 
    layout->addWidget(textEdit); 
    widget->show(); 
    DEBUG_MESSAGE_DISPLAY_WIDGET = widget; 
    DEBUG_MESSAGE_DISPLAY_TEXTEDIT = textEdit; 
    qInstallMsgHandler(debugMessageDisplayFunc); 
} 

void debugMessageDisplayFunc(QtMsgType type, const char *msg) 
{ 
    bool do_abort = false; 
    const char* msgTypeStr = NULL; 
    switch (type) { 
    case QtDebugMsg: 
     msgTypeStr = "Debug"; 
     break; 
    case QtWarningMsg: 
     msgTypeStr = "Warning"; 
     break; 
    case QtCriticalMsg: 
     msgTypeStr = "Critical"; 
     break; 
    case QtFatalMsg: 
     msgTypeStr = "Fatal"; 
     do_abort = true; 
    default: 
     assert(0); 
     return; 
    } 
    QTime now = QTime::currentTime(); 
    QString formattedMessage = 
    QString::fromLatin1("%1 %2 %3") 
    .arg(now.toString("hh:mm:ss:zzz")) 
    .arg(msgTypeStr).arg(msg); 
    // print on console: 
    fprintf(stderr, "%s\n", formattedMessage.toLocal8Bit().constData()); 
    // print in debug log window 
    { 

    bool isMainThread = QThread::currentThread() == QApplication::instance()->thread(); 
    if(DEBUG_MESSAGE_DISPLAY_TEXTEDIT) 
    { 
     if(isMainThread) 
     DEBUG_MESSAGE_DISPLAY_TEXTEDIT->appendPlainText(formattedMessage); 
     else // additional code, so that qDebug calls in threads will work aswell 
     QMetaObject::invokeMethod(DEBUG_MESSAGE_DISPLAY_TEXTEDIT, "appendPlainText", Qt::QueuedConnection, Q_ARG(QString, formattedMessage)); 
    } 
    } 
} 
0

它重定向到visual studio的輸出窗口。

+3

在所有情況下都不是這樣的 –

7

還有一個更簡單的方法:

轉到項目屬性>鏈接>系統:設置子系統 「子系統:CONSOLE」

當您運行程序時,您將獲得一個控制檯,並且qDebug()將被重定向到它。 (你也可以使用std :: cout)

+2

非常奇怪的建議。如果我有一個Win32應用程序,並且/ SUBSYSTEM:WINDOWS是一個強制選項,該怎麼辦? – Dalamber

相關問題