Wxf`s Fantasy World

share something useful


Vscode开发qt

qt creator太难用了,转到vscode开发qt。

新建项目

命令行编译

ok,这是一个完整的流程,接下来用命令行实现 主要用到三个命令:qmake.exe,mingw32-make.exe,gcc.exe
qmake作用是解析xx.pro文件,生成跨平台编译的的makefile文件;
mingw32-make是make命令的windows实现,作用是调用工具链,编译成平台可执行文件,比如exe
gcc,顾名思义,编译器,把代码编译成二进制码

vscode编译

vscode需要用到两个配置:c++、外部指令(qmake、mingw32-make算是外部指令)
安装c++插件,设置智能补全
项目根目录下创建这两个文件,一个是c++编译相关配置,task.json是调用外部指令的配置
配置完成后 : ctrl+shift+p–>run task–>run-debug

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "mkdir", 
            "type": "shell",
            "options": {
                "cwd": "${workspaceFolder}" 
            },
            "command": "mkdir", 
            "args": [ 
                "-Force",
                "build"
            ]
        },
        {
            "label": "qmake-debug",
            "type": "shell",
            "options": {
                "cwd": "${workspaceFolder}/build" 
            },
            "command": "qmake", 
            "args": [ 
                "../${workspaceFolderBasename}.pro", 
                "-spec",
                "win32-g++",
                "\"CONFIG+=debug\"",
                "\"CONFIG+=console\""
            ],
            "dependsOn": [ 
                "mkdir" 
            ]
        },
        {
            "label": "make-debug",
            "type": "shell",
            "options": {
                "cwd": "${workspaceFolder}/build"
            },
            "command": "mingw32-make",
            "args": [
                "-f",
                "Makefile.Debug", 
                "-j7" 
            ],
            "dependsOn": [
                "qmake-debug"
            ]
        },
        {
            "label": "run-debug",
            "type": "process", 
            "options": {
                "cwd": "${workspaceFolder}/build/debug" 
            },
            "command": "${workspaceFolderBasename}.exe", 
            "dependsOn": [
                "make-debug"
            ]
        },
        {
            "label": "qmake-release",
            "type": "shell",
            "options": {
                "cwd": "${workspaceFolder}/build"
            },
            "command": "qmake",
            "args": [ 
                "../${workspaceFolderBasename}.pro",
                "-spec",
                "win32-g++",
                "\"CONFIG+=qtquickcompiler\""
            ],
            "dependsOn": [
            ]
        },
        {
            "label": "make-release",
            "type": "shell",
            "options": {
                "cwd": "${workspaceFolder}/build"
            },
            "command": "mingw32-make",
            "args": [
                "-f",
                "Makefile.Release", 
                "-j7"
            ],
            "dependsOn": [
                "qmake-release"
            ]
        },
        {
            "label": "run-release",
            "type": "process",
            "options": {
                "cwd": "${workspaceFolder}/build/release"
            },
            "command": "${workspaceFolderBasename}.exe",
            "dependsOn": [
                "make-release"
            ]
        },
        {
            "label": "clean",
            "type": "shell",
            "options": {
                "cwd": "${workspaceFolder}/build"
            },
            "command": "mingw32-make",
            "args": [
                "clean"
            ]
        }
    ]
}

注意点

1 windows下不要在类linux系统执行,code打开项目,会出现路径解析错误
2 QML文件好好写,弹窗出不来,找了半天原因,发现qml写的不对