在vscode中使用cmake构建Qt6项目

0. 环境

环境

  • Win11+vscode
  • CMake: 3.26.1
  • Qt: 6.5.1

1. QT6的安装

参考文章:Qt6安装教程——国内源
下载网址:qt在线安装工具(阿里云盘)

  • 在cmd中打开exe文件,方便添加镜像参数
    参数
  • 注册QT账户、登录QT账户
  • 勾选组件
    选择组件
  • MinGW 11.2.0.64-bit
  • Qt Quick 3D : QML3d效果
  • Qt5兼容模块
  • Qt Debug Information Files: Qt调试工具
  • Qt Quick Timeline: QML时间轴工具(用于特效)
  • 源码一般不需要,web应用和arm还在技术预览阶段一般不需要

2. 编写项目

参考文章:如何使用VScode搭建Qt开发环境
实现一个textFinder控件,能够实现文章中的关键词搜索

1. 新建文件夹QtTest

mkdir

2. 项目结构

项目结构

3. 文件内容

(1) CMakeLists.txt

cmakeLists.txt

cmake_minimum_required(VERSION 3.0.0)
project(textFinder VERSION 0.1.0 LANGUAGES C CXX)
# QT相关
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(QT6_PATH "D:\\component\\QT\\6.5.1\\mingw_64\\lib\\cmake")
set(PROJECT_RESOURCE ${PROJECT_SOURCE_DIR}/resources/res.qrc)
set(CMAKE_PREFIX_PATH ${QT6_PATH})
find_package(Qt6 REQUIRED COMPONENTS Widgets)
qt_standard_project_setup()
add_subdirectory(src/app)
add_subdirectory(src/textFinder)

(2) src/textFinder/CMakeLists.txt

text

add_library(textFinder STATIC textFinder.cpp)
target_link_libraries(textFinder PRIVATE Qt6::Widgets)
target_include_directories(textFinder INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})

(3) src/app/CMakeLists.txt

app

aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCE)
add_executable(demo)
target_link_libraries(demo PRIVATE Qt6::Widgets textFinder)
target_sources(demo PUBLIC ${APP_SOURCE} ${PROJECT_RESOURCE})
set_target_properties(demo PROPERTIES
    WIN32_EXECUTABLE ON
    MACOSX_BUNDLE ON
)

(4)resources/res.qrc

<RCC>
    <qresource prefix="/">
        <file>input.txt</file>
    </qresource>
</RCC>

(5)resources/input.txt: 任意文本

(6)src/textFinder/textFinder.h

#ifndef _TEXTFINDER_H_
#define _TEXTFINDER_H_
#include <QLineEdit>
#include <QTextEdit>
#include <QWidget>
class TextFinder : public QWidget {
    Q_OBJECT
public:
    TextFinder (QWidget *parent = nullptr) ;
    ~TextFinder();
private:
    QTextEdit *m_pTextEdit;
    QLineEdit *m_pLineEdit;
    //初始化UI
    void initUI();
    //加载文本文件
    void loadTextFile();
private slots:
    void onBtnFindClicked();
};
#endif

(7)src/textFinder/textFinder.cpp

#include "textFinder.h"
#include <QFile>
#include <QLayout>
#include <QPushButton>
#include <QTextStream>
TextFinder::TextFinder(QWidget *parent) : QWidget(parent) {
    initUI();
    loadTextFile();
}
TextFinder::~TextFinder() = default;
void TextFinder::initUI(){
    auto hBox = new QHBoxLayout();
    auto vBox = new QVBoxLayout();
    auto btnFind = new QPushButton("Find", this);
    m_pLineEdit = new QLineEdit(this);
    m_pTextEdit = new QTextEdit(this);
    //设置textFinder的组件布局为垂直布局
    setLayout(vBox);
    m_pLineEdit -> setPlaceholderText("Keyword:");
    hBox->addWidget(m_pLineEdit, 2);
    hBox->addWidget(btnFind, 0);
    vBox->addLayout(hBox);
    vBox->addWidget(btnFind, 0);
    vBox->addLayout(hBox);
    vBox->addWidget(m_pTextEdit);
    connect(btnFind, &QPushButton::clicked, this, &TextFinder::onBtnFindClicked);
}
void TextFinder::loadTextFile(){
    QFile inputFile(":/input.txt");
    inputFile.open(QIODevice::ReadOnly);
    QTextStream in(&inputFile);
    QString line = in.readAll();
    inputFile.close();
    m_pTextEdit -> setPlainText(line);
    QTextCursor cursor = m_pTextEdit->textCursor();
    cursor.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor,1);
}
void TextFinder::onBtnFindClicked(){
    qDebug() << "btnFind has been clicked";
    auto keyword = m_pLineEdit->text();
    m_pTextEdit -> find(keyword, QTextDocument::FindWholeWords);
}

(8)src/app/main.cpp

#include <textFinder.h>
#include <QApplication>
int main(int argc, char** argv){
    QApplication a(argc,argv);
    TextFinder w;
    w.show();
    return a.exec();
}

3. 构建CMake

cmake:quick start

在这里插入图片描述

选择Kit

选择kit

如果找不到Qt安装目录下的mingw,则需要到设置中手动添加
手动添加

  • 而后,vscode将自动构建CMake工程

build

4. vscode执行可执行文件

(1).vscode/tasks.json

{
    "version": "2.0.0",
    "tasks" : [
        {
            "type" : "cmake",
            "label": "CMAKE: clean build",
            "command" : "cleanRebuild",
            "targets" : [
                "demo"
            ],
            "group":{
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": [],
            "detail": "CMake template clean rebuild task"
        }
    ]
}

(2).vscode/launch.json

launch

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "cppdbg",
            "request": "launch",
            "name": "Demo",
            "program": "${command:cmake.launchTargetPath}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${command:cmake.launchTargetDirectory}",
            "preLaunchTask": "CMake: clean rebuild",
            "environment": [
                {
                    "name": "PATH",
                    "value": "d:/component/QT/6.5.1/mingw_64/bin"
                }
            ],
            "externalConsole": false,
            "miDebuggerPath": "d:/component/QT/Tools/mingw1120_64/bin/gdb.exe",
        }
    ]
}

(3) .vscode/settings.json

{
    "qtConfigure.qtKitDir": "d:/component/QT/6.5.1/mingw_64"
}

(4)F5调试运行

调试运行

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值