• 12642阅读
  • 0回复

【原创】将qt for windows的qDebug信息输出到控制台 [复制链接]

上一主题 下一主题
离线chenshijie
 

只看楼主 倒序阅读 楼主  发表于: 2005-10-08
Qt for Windows, qDebug信息是输出到调试器的,不太方便,我写了个函数,可以打开一个console输出qDebug信息,在QT Windows Free Edition 3.3.4 for msvc下通过。

/****************************************************************
**
** Qt tutorial 1
**
****************************************************************/

#include <qapplication.h>
#include <qpushbutton.h>

#include <windows.h>
//#include <stdio.h>
//#include <stdlib.h>
//#include <stdarg.h>

void mydebug(const char *format, ...)
{
  static int isInit = 0;
  static char strbuf[8196];

  va_list argList;
  va_start(argList, format);
  vsprintf(strbuf, format, argList);
  va_end(argList);

  if (!isInit)
  {
    AllocConsole();
    isInit = 1;
  }

  HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
  DWORD n, m;
  WriteConsoleA(h, strbuf, strlen(strbuf), &n, &m);   //call non-unicode WriteConsoleA
}

void myMessageOutput( QtMsgType type, const char *msg )
{
  switch ( type ) {
  case QtDebugMsg:
    mydebug("%s\n", msg );
    break;
  case QtWarningMsg:
    mydebug("Warning: %s\n", msg );
    break;
  case QtFatalMsg:
    mydebug("Fatal: %s\n", msg );
    abort(); // deliberately core dump
  }
}


int main( int argc, char ** argv )
{
  qInstallMsgHandler( myMessageOutput ); // Installs a Qt message handler

  QApplication a( argc, argv );

  QPushButton hello( "Hello world!", 0 );
  hello.resize( 100, 30 );

  a.setMainWidget( &hello );
  hello.show();

  qDebug("Hello!");
 
  return a.exec();
}
[ 此贴被fanyu在2005-10-08 14:17重新编辑 ]
快速回复
限100 字节
 
上一个 下一个