#include <qapplication.h>
#include <iostream>
#include <qglobal.h>
//QT调试技术
void check_assert(int size);
void myMessageOutput( int input );
int main( int argc, char **argv ) {
QApplication a( argc, argv );
int temp;
while (1) {
std::cout << "Please input 1 2 or 3 here and else for quit:";
std::cin >> temp;
myMessageOutput( temp );
check_assert(temp);
}
return a.exec();
}
/*
使用警告和调试信息
qDebug()
qWarning()
qFatal()
*/
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为空时,将输出警告信息并指出代码所在的文件和行数
*/
void check_assert(int size) {
//ASSERT( ( size > 0) );
Q_ASSERT( ( size > 0) );
char* p= NULL;
if ( size > 5 )
p = new char(size);
if ( size < 0 ) {
std::cout<<"It will be quit."<< std::endl ;
abort();
} else
//CHECK_PTR(p);
Q_CHECK_PTR(p);
}
/*
参考资料:
学习qt调试
Author :魏自立
*/