QtMvvm 开源项目教程

QtMvvm 开源项目教程

QtMvvmA mvvm oriented library for Qt, to create Projects for Widgets and Quick in parallel项目地址:https://gitcode.com/gh_mirrors/qt/QtMvvm

1. 项目的目录结构及介绍

QtMvvm 项目的目录结构如下:

QtMvvm/
├── CMakeLists.txt
├── LICENSE
├── README.md
├── examples/
│   ├── CMakeLists.txt
│   ├── core_example/
│   ├── quick_example/
│   └── widgets_example/
├── include/
│   ├── QtMvvm/
│   └── qtmvvm_export.h
├── src/
│   ├── CMakeLists.txt
│   ├── core/
│   ├── quick/
│   └── widgets/
└── tests/
    ├── CMakeLists.txt
    ├── core/
    ├── quick/
    └── widgets/

目录介绍

  • CMakeLists.txt: 项目的 CMake 构建文件。
  • LICENSE: 项目的许可证文件。
  • README.md: 项目的说明文档。
  • examples/: 包含项目的示例代码,分为 core_example, quick_example, 和 widgets_example
  • include/: 包含项目的头文件,主要在 QtMvvm 目录下。
  • src/: 包含项目的源代码,分为 core, quick, 和 widgets 三个部分。
  • tests/: 包含项目的测试代码,分为 core, quick, 和 widgets 三个部分。

2. 项目的启动文件介绍

QtMvvm 项目的启动文件通常位于 examples/ 目录下的各个示例项目中。以 quick_example 为例,其启动文件为 main.cpp

quick_example/main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QtMvvmCore/ServiceRegistry>
#include <QtMvvmQuick/QuickPresenter>
#include <QtMvvmQuick/QuickView>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    // 初始化 QtMvvm 服务
    qtmvvm_init();

    QQmlApplicationEngine engine;
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);

    return app.exec();
}

启动文件介绍

  • QGuiApplication: 创建一个 Qt 图形用户界面应用程序。
  • QQmlApplicationEngine: 加载 QML 文件并运行应用程序。
  • qtmvvm_init(): 初始化 QtMvvm 服务。
  • QuickPresenterQuickView: 用于处理 QML 视图和数据绑定。

3. 项目的配置文件介绍

QtMvvm 项目的配置文件通常位于项目的根目录或 examples/ 目录下的各个示例项目中。以 quick_example 为例,其配置文件为 CMakeLists.txt

quick_example/CMakeLists.txt

cmake_minimum_required(VERSION 3.14)

project(quick_example VERSION 0.1 LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)

find_package(Qt5 REQUIRED COMPONENTS Core Quick)
find_package(QtMvvm REQUIRED)

add_executable(quick_example
    main.cpp
    main.qml
)

target_link_libraries(quick_example
    Qt5::Core
    Qt5::Quick
    QtMvvm::Core
    QtMvvm::Quick
)

install(TARGETS quick_example
    RUNTIME DESTINATION bin
)

配置文件介绍

  • cmake_minimum_required: 指定所需的 CMake 最低版本。
  • project: 定义项目名称和版本。
  • set: 设置 CMake 变量。
  • find_package: 查找并加载 Qt 和 QtMvvm 包。
  • add_executable: 定义可执行文件及其源文件。

QtMvvmA mvvm oriented library for Qt, to create Projects for Widgets and Quick in parallel项目地址:https://gitcode.com/gh_mirrors/qt/QtMvvm

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

童兴富Stuart

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值