1. Use qDebug to print key information and view it in the program output window, such as:
void QtAppDebug::on_pushButton_clicked()
{
qDebug() << "---QtAppDebug---" << endl;
}
2. Use QMessageBox to display key information, and view through the window during program operation, such as:
void QtAppDebug::on_pushButton_clicked()
{
QMessageBox::information(this,"Test","---QtAppDebug---");
}
3. Annotate the suspicious code, gradually narrow the scope according to the order of module functions, and accurately locate the bug code, such as:
void QtAppDebug::on_pushButton_clicked()
{
#if 0
QMessageBox::information(this,"Test","---QtAppDebug---");
#endif
}
4. qtcreator uses F10, F11, F5 for single-step debugging to view key data information during program operation;
5. Print debugging information to the log file for analysis, use examples , a more comprehensive system log framework can learn Log4Qt;
6. When debugging the complex interface display, you can set the color of the control to clearly view the size and boundary of the control, as follows:
void QtAppDebug::on_pushButton_clicked()
{
ui->pushButton->setStyleSheet("background-color:red");
}
7. Add exception capture code, generate dump file when crash, analyze and locate according to dump file, as follows:
//异常捕获函数
long ApplicationCrashHandler(EXCEPTION_POINTERS *pException){
{
// 在程序exe的上级目录中创建dmp文件夹
QDir *dmp = new QDir;
bool exist = dmp->exists("../dmp/");
if(exist == false)
dmp->mkdir("../dmp/");
}
QDateTime current_date_time = QDateTime::currentDateTime();
QString current_date = current_date_time.toString("yyyy_MM_dd_hh_mm_ss");
QString time = current_date + ".dmp";
EXCEPTION_RECORD *record = pException->ExceptionRecord;
QString errCode(QString::number(record->ExceptionCode, 16));
QString errAddr(QString::number((uint)record->ExceptionAddress, 16));
QString errFlag(QString::number(record->ExceptionFlags, 16));
QString errPara(QString::number(record->NumberParameters, 16));
qDebug()<<"errCode: "<<errCode;
qDebug()<<"errAddr: "<<errAddr;
qDebug()<<"errFlag: "<<errFlag;
qDebug()<<"errPara: "<<errPara;
HANDLE hDumpFile = CreateFile((LPCSTR)QString("../dmp/" + time).utf16(),
GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if(hDumpFile != INVALID_HANDLE_VALUE) {
MINIDUMP_EXCEPTION_INFORMATION dumpInfo;
dumpInfo.ExceptionPointers = pException;
dumpInfo.ThreadId = GetCurrentThreadId();
dumpInfo.ClientPointers = TRUE;
MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(),hDumpFile, MiniDumpNormal, &dumpInfo, NULL, NULL);
CloseHandle(hDumpFile);
}
else{
qDebug()<<"hDumpFile == null";
}
return EXCEPTION_EXECUTE_HANDLER;
}```
//注冊异常捕获函数
SetUnhandledExceptionFilter((LPTOP_LEVEL_EXCEPTION_FILTER)ApplicationCrashHandler);