Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to view qDebug() in Visual Studio?

In Qt Creator, I could view the qDebug(), qWarning() etc. output directly in the IDE. How could I do this in Visual Studio?

like image 324
Jichao Avatar asked Jun 25 '12 06:06

Jichao


People also ask

Where can I see QDebug?

While running your application click the Application Output tab Then you will see all your debugs message if you used a "qDebug()" macro.

What is QDebug function?

QDebug is used whenever the developer needs to write out debugging or tracing information to a device, file, string or console.


2 Answers

There is an easier way:

Go to Project Properties > Linker > System : set SubSystem to "SUBSYSTEM:CONSOLE"

You will now get a console when you run your program and qDebug() will be redirected to it. (You can also use std::cout)

like image 124
user929404 Avatar answered Sep 25 '22 19:09

user929404


When you run the program with an attached debugger, it will show in the Output Window of Visual Studio, but for Debug purposes i often redirect the debug output to some kind of nice log window, which you can do by using the function qInstallMsgHandler:

the code that i use:

#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 ) );
    }
  }
}
like image 20
smerlin Avatar answered Sep 24 '22 19:09

smerlin