VS Code for Qt Applications - Part 2 - KDAB
.vscode配置
1. tasks.json配置
1.1 配置Debug文件夹的路径
```json
{
"label": "Create build dir",
"type": "shell",
"command": "mkdir -Force path"
},
// path是指定的文件夹,qmake生成的debug文件都存放在这里
```
1.2 配置qmake
```json
{
"label": "Run qmake",
"type": "shell",
"command": "qmake",
"args": [
"../${workspaceRootFolderName}.pro"
],
"options": {
"cwd": "path" // 相当于在当前项目的path文件夹下执行qmake,path与1.1一致
}
},
```
1.3 配置make
```json
{
"label": "Run make",
"type": "shell",
"command": "make", // window系统为mingw32-make,我这里是重命名了
"args": [
"-f",
"Makefile.Debug"
],
"options": {
"cwd": ".exe"
}
},
```
1.4 配置构建命令
```json
{
"label": "Build",
"dependsOn": [
"Create build dir",
"Run qmake",
"Run make"
],
"dependsOrder": "sequence"
},
```
2. 配置launch.json
```json
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/path/debug/${workspaceRootFolderName}.exe",// 这里的path与1.1设置的path对应
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "D:/CTools/Qt/Qt5.9.8/Tools/mingw530_32/bin/gdb.exe",// 这里的gdb要取Qt自带的
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "Build" ·//先执行tasks中的Build,与1.4对应
}
]
}
```
qDebug()无法打印
.pro文件添加CONFIG += console,没用。。。。。。
用某位大佬的代码,将qDebug信息重定向到日志文件:
```c++
// 在main.cpp中使用setDebugOutput("./debug.log");开启
void setDebugOutput(const QString &rawTargetFilePath_, const bool &argDateFlag_)
{
static QString rawTargetFilePath;
static bool argDateFlag;
rawTargetFilePath = rawTargetFilePath_;
argDateFlag = argDateFlag_;
class HelperClass
{
public:
static void messageHandler(QtMsgType type, const QMessageLogContext &, const QString &message_)
{
QString message;
switch ( type )
{
case QtDebugMsg:
{
message = message_;
break;
}
case QtWarningMsg:
{
message.append("Warning: ");
message.append(message_);
break;
}
case QtCriticalMsg:
{
message.append("Critical: ");
message.append(message_);
break;
}
case QtFatalMsg:
{
message.append("Fatal: ");
message.append(message_);
break;
}
default: { break; }
}
QString currentTargetFilePath;
if ( argDateFlag )
{
currentTargetFilePath = rawTargetFilePath.arg( ( ( argDateFlag ) ? ( QDateTime::currentDateTime().toString("yyyy_MM_dd") ) : ( "" ) ) );
}
else
{
currentTargetFilePath = rawTargetFilePath;
}
if ( !QFileInfo::exists( currentTargetFilePath ) )
{
QDir().mkpath( QFileInfo( currentTargetFilePath ).path() );
}
QFile file( currentTargetFilePath );
file.open( QIODevice::WriteOnly | QIODevice::Append );
QTextStream textStream( &file );
textStream << QDateTime::currentDateTime().toString( "yyyy-MM-dd hh:mm:ss" ) << ": " << message << endl;
}
};
qInstallMessageHandler( HelperClass::messageHandler );
}
```