qt学习 - 第一个项目ubuntu下vscode+qmake

参考:
Compiling your first Qt Program in Ubuntu
Vscode开发Qt
Visual Studio Code开发Qt应用程序实现自动提示、语法高亮、代码导航、编译和调试

安装环境

  1. In order to install the Build Essential package: $ sudo apt-get install build-essential
  2. In order to install the Qt Creator package that contains both the UI and command line tools for Qt project creation and execution:
    $ sudo apt-get install qtcreator
  3. If you want Qt 5 to be used as the default Qt Creator version, then run the following command:
    $ sudo apt install qt5-default
  4. Install the Qt documentation and examples:
    $ sudo apt-get install qt5-doc qtbase5-examples qtbase5-doc-html

vscode 配置

  1. 创建目录做为workspace,就叫qt吧。
  2. 按照指引或手动创建配置文件,以实现代码导航
    • .vscode/c_cpp_properties.json:
      {
          "configurations": [
              {
                  "name": "Linux",
                  "includePath": [
                      "${workspaceFolder}/**",
                      "/usr/include/x86_64-linux-gnu/qt5/QtWidgets",
                      "/usr/include/x86_64-linux-gnu/qt5/*"
                  ],
                  "defines": [],
                  "compilerPath": "/usr/bin/clang",
                  "cStandard": "c11",
                  "cppStandard": "c++17",
                  "intelliSenseMode": "clang-x64"
              }
          ],
          "version": 4
      }
      
  3. 创建cpp文件
    • testmain.cpp:
        #include 
        #include 
        #include 
        
        int main(int argc, char *argv[])
        {
            QApplication app(argc, argv);
            QLabel hello("
      Welcome to my first Qt program
      "
      ); hello.setWindowTitle("My First Qt Program"); hello.resize(400, 400); hello.show(); return app.exec(); }
  4. 使用qmake生成qt工程
    $ qmake -project
    会生成一个qt.pro文件,文件名规则是 ‘工程目录名’.pro 。
    其中有一行声明了目标文件:TARGET = qt
    我们在里面添加一行:QT += gui widgets 应该是声明依赖的作用。
  5. 使用qmake生成Makefile
    $ qmake qt.pro
  6. 编译
    $ make 即可生成可执行文件,文件名是pro文件制定的TARGET。
  7. 运行一下看看
    $ chmod +x qt && ./qt ,会弹出一个窗口:
    qt学习 - 第一个项目ubuntu下vscode+qmake_第1张图片
  8. 使用vscode的run功能:
    • 新建 launch.json 文件:
      {
          // Use IntelliSense to learn about possible attributes.
          // Hover to view descriptions of existing attributes.
          // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
          "version": "0.2.0",
          "configurations": [
              {
                  "name": "(gdb) Launch",
                  "type": "cppdbg",
                  "request": "launch",
                  "program": "${workspaceFolder}/qt",
                  "args": [],
                  "stopAtEntry": false,
                  "cwd": "${workspaceFolder}",
                  "environment": [],
                  "externalConsole": false,
                  "MIMode": "gdb",
                  "setupCommands": [
                      {
                          "description": "Enable pretty-printing for gdb",
                          "text": "-enable-pretty-printing",
                          "ignoreFailures": true
                      }
                  ],
                  "preLaunchTask": "Build"
              }
          ]
      }
      
      其中有一行 "preLaunchTask": "Build" , 这个Build会在tasks.json文件里找定义。
    • 新建 tasks.json 文件
      {
          "version": "2.0.0",
          "tasks": [
              {
                  "label": "Build",                              // build任务
                  "type": "shell",
                  // "command": "make -f Makefile"
                  "command" : "qmake"
              }
          ]
      }
      
      这时就可以在vscode的run中点击运行了。
  9. 看下现在的目录结构:
    ├── Makefile
    ├── .qmake.stash
    ├── qt
    ├── qt.pro
    ├── testmain.cpp
    ├── testmain.o
    └── .vscode
    	├── c_cpp_properties.json
    	├── launch.json
    	├── settings.json		 // 没啥意义,vscode提示自动生成的
    	└── tasks.json
    
    1 directory, 10 files
    

你可能感兴趣的:(qt学习)