Window下Qt中用qDebug()输出调试信息到Console控制台的设置方法
2012-02-08 16:37
447 查看
在QT的学习过程中,调试是非常重要的,单步调试慢慢的让你更容易理解
学习qt调试
1. 使用警告和调试信息
qDubug():输出调试信息
Example:
qDebug( "my window handle = %x", myWidget->id() );
qWarning():输出警告信息
Example:
void f( int c )
{
if ( c > 200 )
qWarning( "f: bad argument, c == %d", c );
}
qFatal():输出致命错误信息 ,程序自动被迫中止
Example:
int divide( int a, int b )
{
if ( b == 0 ) // program error
qFatal( "divide: cannot divide by zero" );
return a/b;
}
使用例子:
#include <qapplication.h>
#include <iostream.h>
void myMessageOutput( int input );
int main( int argc, char **argv )
{
QApplication a( argc, argv );
int temp;
while(1)
{
cout << "Please input 1 2 or 3 here and else for quit:";
cin>>temp;
myMessageOutput( temp ) ;
}
return a.exec();
}
void myMessageOutput( int input )
{
switch ( input ) {
case 1:
qDebug("The number you input is : %d ",input);
break;
case 2:
qWarning("The number you input is : %d ",input);
break;
case 3:
qWarning("The number you input is : %d ",input);
break;
default :
//qWarning("The number you input is : %d ,which is invalid here.",input);
qFatal( "It will be quit." );
//cout<<"It will be quit."<<endl ;
//abort();
}
}
2. 使用调试宏
头文件qglobal.h 中定义了一些用于调试的宏:
Q_ASSERT(BOOL b) :b为假时程序将输出警告信息并指出代码所在的文件和行数
Q_CHECK_PTR(point):point为一个指针变量,如果point为空时,将输出警告信息并指出代码所在的文件和行数
ASSERT(BOOL b)和CHECK_PTR(point)是标准C++中的语法。
#include <qapplication.h>
#include <iostream.h>
//#include <qglobal.h>
void check_assert(int size);
int main( int argc, char **argv )
{
QApplication a( argc, argv );
int temp;
while(1)
{
cout << "Please input a number here which should be more than 5 ,or less 0 quit:";
cin>>temp;
check_assert(temp);
}
return a.exec();
}
void check_assert(int size)
{
ASSERT( ( size > 0) );
char* p= NULL;
if ( size > 5 )
p = new char(size);
if ( size < 0 )
{
cout<<"It will be quit."<<endl ;
abort();
}
else
CHECK_PTR(p);
}
学习qt调试
1. 使用警告和调试信息
qDubug():输出调试信息
Example:
qDebug( "my window handle = %x", myWidget->id() );
qWarning():输出警告信息
Example:
void f( int c )
{
if ( c > 200 )
qWarning( "f: bad argument, c == %d", c );
}
qFatal():输出致命错误信息 ,程序自动被迫中止
Example:
int divide( int a, int b )
{
if ( b == 0 ) // program error
qFatal( "divide: cannot divide by zero" );
return a/b;
}
使用例子:
#include <qapplication.h>
#include <iostream.h>
void myMessageOutput( int input );
int main( int argc, char **argv )
{
QApplication a( argc, argv );
int temp;
while(1)
{
cout << "Please input 1 2 or 3 here and else for quit:";
cin>>temp;
myMessageOutput( temp ) ;
}
return a.exec();
}
void myMessageOutput( int input )
{
switch ( input ) {
case 1:
qDebug("The number you input is : %d ",input);
break;
case 2:
qWarning("The number you input is : %d ",input);
break;
case 3:
qWarning("The number you input is : %d ",input);
break;
default :
//qWarning("The number you input is : %d ,which is invalid here.",input);
qFatal( "It will be quit." );
//cout<<"It will be quit."<<endl ;
//abort();
}
}
2. 使用调试宏
头文件qglobal.h 中定义了一些用于调试的宏:
Q_ASSERT(BOOL b) :b为假时程序将输出警告信息并指出代码所在的文件和行数
Q_CHECK_PTR(point):point为一个指针变量,如果point为空时,将输出警告信息并指出代码所在的文件和行数
ASSERT(BOOL b)和CHECK_PTR(point)是标准C++中的语法。
#include <qapplication.h>
#include <iostream.h>
//#include <qglobal.h>
void check_assert(int size);
int main( int argc, char **argv )
{
QApplication a( argc, argv );
int temp;
while(1)
{
cout << "Please input a number here which should be more than 5 ,or less 0 quit:";
cin>>temp;
check_assert(temp);
}
return a.exec();
}
void check_assert(int size)
{
ASSERT( ( size > 0) );
char* p= NULL;
if ( size > 5 )
p = new char(size);
if ( size < 0 )
{
cout<<"It will be quit."<<endl ;
abort();
}
else
CHECK_PTR(p);
}
相关文章推荐
- Window下Qt中用qDebug()输出调试信息到Console控制台的设置方法
- Window下Qt中用qDebug()输出调试信息到Console控制台的设置方法
- Window下Qt中用qDebug()输出调试信息到Console控制台的设置方法
- Window下Qt中用qDebug()输出调试信息到Console控制台的设置方法(转)
- qt在GUI显示时,将调试信息输出到控制台的设置
- MFC利用控制台输出调试信息的方法
- Qt5使用qDebug()在windows控制台中输出信息
- 如何在浏览器控制台(console)里输出彩色样式调试信息
- MFC(win32GUI)程序中显示Dos调试信息.console.显示控制台界面.标准输出.
- Struts2框架调试时,控制台打印输出大量<mime-mapping>相关信息,解决方法
- 用qDebug调试输出QT控制台程序(windows)
- Qt 使用#define+qDebug()输出调试信息
- vc6.0或者vs2008 MFC程序采用console输出调试信息的方法
- Qt在windows下在控制台用qDebug()输出调试错误
- 在MFC,Win32程序中向控制台(Console)窗口输出调试信息
- 如何在浏览器控制台(console)里输出彩色样式调试信息
- php在eclipse的控制台console输出调试信息功能实现
- php在eclipse的控制台console输出调试信息功能实现
- 在MFC,Win32程序中向控制台(Console)窗口输出调试信息
- MFC,Win32程序向控制台(Console)窗口输出调试信息