Qt Quick 教程(二)

文章目录

今天分析一段代码

cpp 复制代码
    // Register types
    qmlRegisterSingletonType(QUrl("qrc:/StyleSheet.qml"), "Librum.style", 1, 0, "Style");
    qmlRegisterSingletonType(QUrl("qrc:/IconSheet.qml"), "Librum.icons", 1, 0, "Icons");
    qmlRegisterSingletonType(QUrl("qrc:/FontSheet.qml"), "Librum.fonts", 1, 0, "Fonts");
    qmlRegisterSingletonType(QUrl("qrc:/Globals.qml"), "Librum.globals", 1, 0, "Globals");
    qmlRegisterSingletonType(QUrl("qrc:/GlobalSettings.qml"), "Librum.globalSettings", 1, 0, "GlobalSettings");
    qmlRegisterSingletonType(QUrl("qrc:/TranslationsModel.qml"), "Librum.models", 1, 0, "TranslationsModel");
    qmlRegisterSingletonType(QUrl("qrc:/modules/CustomComponents/MLanguageModel.qml"), "Librum.models", 1, 0, "MLanguageModel");
    qmlRegisterType<adapters::data_models::LibraryProxyModel>("Librum.models", 1, 0, "LibraryProxyModel");
    qmlRegisterType<adapters::data_models::BookTitleProxyModel>("Librum.models", 1, 0, "BookTitleModel");
    qmlRegisterType<adapters::data_models::FreeBooksModel>("Librum.models", 1, 0, "FreeBooksModel");
    qmlRegisterType<adapters::data_models::ShortcutsProxyModel>("Librum.models", 1, 0, "ShortcutsProxyModel");
    qmlRegisterType<cpp_elements::KeySequenceRecorder>("Librum.elements", 1, 0, "KeySequenceRecorder");
    qmlRegisterType<cpp_elements::PageView>("Librum.elements", 1, 0, "PageView");
    qRegisterMetaType<adapters::dtos::BookDto>();
    qRegisterMetaType<adapters::dtos::TagDto>();
    qRegisterMetaType<adapters::dtos::FolderDto>();
    qRegisterMetaType<adapters::dtos::DictionaryEntryDto>();
    qRegisterMetaType<adapters::dtos::WordTypeDto>();
    qRegisterMetaType<adapters::dtos::WordDefinitionDto>();

1. 注册单例类型

  • qmlRegisterSingletonType<>() 是一个 C++ 函数模板,用于在 Qt Quick 中注册单例类型,使得在 QML 中可以直接访问和使用这些单例对象。

  • 使用 qmlRegisterSingletonType<>() 可以将一个 C++ 类注册为单例类型,这意味着在整个 QML 应用程序中,只会创建一个该类的实例,并且该实例可以在 QML 中全局访问。这使得在 QML 中可以方便地使用单例对象,无需显式创建实例,而是通过直接引用单例类型来使用它。

  • 这些代码将QML文件注册为单例类型,使得这些类型可以在QML文件中以单例的形式使用。注册的类型可以通过Librum.style.Style、Librum.icons.Icons等名称在QML中访问。

cpp 复制代码
qmlRegisterSingletonType(QUrl("qrc:/StyleSheet.qml"), "Librum.style", 1, 0, "Style");
qmlRegisterSingletonType(QUrl("qrc:/IconSheet.qml"), "Librum.icons", 1, 0, "Icons");
qmlRegisterSingletonType(QUrl("qrc:/FontSheet.qml"), "Librum.fonts", 1, 0, "Fonts");
qmlRegisterSingletonType(QUrl("qrc:/Globals.qml"), "Librum.globals", 1, 0, "Globals");
qmlRegisterSingletonType(QUrl("qrc:/GlobalSettings.qml"), "Librum.globalSettings", 1, 0, "GlobalSettings");
qmlRegisterSingletonType(QUrl("qrc:/TranslationsModel.qml"), "Librum.models", 1, 0, "TranslationsModel");
qmlRegisterSingletonType(QUrl("qrc:/modules/CustomComponents/MLanguageModel.qml"), "Librum.models", 1, 0, "MLanguageModel");
  • 用法一(C++代码):
cpp 复制代码
 // First, define the singleton type provider function (callback).
 static QJSValue example_qjsvalue_singletontype_provider(QQmlEngine *engine, QJSEngine *scriptEngine)
 {
     Q_UNUSED(engine)

     static int seedValue = 5;
     QJSValue example = scriptEngine->newObject();
     example.setProperty("someProperty", seedValue++);
     return example;
 }

 // Second, register the singleton type provider with QML by calling this function in an initialization function.
 qmlRegisterSingletonType("Qt.example.qjsvalueApi", 1, 0, "MyApi", example_qjsvalue_singletontype_provider);
  • 用法二,同理,可以使用C++11 lambda:
cpp 复制代码
 qmlRegisterSingletonType("Qt.example.qjsvalueApi", 1, 0, "MyApi", [](QQmlEngine *engine, QJSEngine *scriptEngine) -> QJSValue {
     Q_UNUSED(engine)

     static int seedValue = 5;
     QJSValue example = scriptEngine->newObject();
     example.setProperty("someProperty", seedValue++);
     return example;
 });
  • QML中,导入
sh 复制代码
 import QtQuick 2.0
 import Qt.example.qjsvalueApi 1.0 as ExampleApi
 Item {
     id: root
     property int someValue: ExampleApi.MyApi.someProperty
 }

2. 注册普通QML类型

这些代码将C++类注册为QML类型,使得这些类型可以在QML文件中使用。例如,LibraryProxyModel可以通过Librum.models.LibraryProxyModel在QML中实例化。

cpp 复制代码
qmlRegisterType<adapters::data_models::LibraryProxyModel>("Librum.models", 1, 0, "LibraryProxyModel");
qmlRegisterType<adapters::data_models::BookTitleProxyModel>("Librum.models", 1, 0, "BookTitleModel");
qmlRegisterType<adapters::data_models::FreeBooksModel>("Librum.models", 1, 0, "FreeBooksModel");
qmlRegisterType<adapters::data_models::ShortcutsProxyModel>("Librum.models", 1, 0, "ShortcutsProxyModel");
qmlRegisterType<cpp_elements::KeySequenceRecorder>("Librum.elements", 1, 0, "KeySequenceRecorder");
qmlRegisterType<cpp_elements::PageView>("Librum.elements", 1, 0, "PageView");

3. 注册C++类型到Qt元对象系统

这些代码将C++类型注册到Qt元对象系统中,使得这些类型可以在信号槽机制中使用。这对于在C++和QML之间传递这些类型的对象是必要的。

cpp 复制代码
qRegisterMetaType<adapters::dtos::BookDto>();
qRegisterMetaType<adapters::dtos::TagDto>();
qRegisterMetaType<adapters::dtos::FolderDto>();
qRegisterMetaType<adapters::dtos::DictionaryEntryDto>();
qRegisterMetaType<adapters::dtos::WordTypeDto>();
qRegisterMetaType<adapters::dtos::WordDefinitionDto>();

4.总结,具体解释

  • qmlRegisterSingletonType:注册一个QML文件作为单例类型。这个QML文件将在整个应用程序中共享一个实例。例如,qmlRegisterSingletonType(QUrl("qrc:/StyleSheet.qml"), "Librum.style", 1, 0, "Style"); 会注册一个名为Style的单例类型,该类型定义在StyleSheet.qml文件中,并且可以通过Librum.style.Style在QML中访问。

  • qmlRegisterType:将C++类注册为QML类型,使得该类型可以在QML文件中实例化。例如,qmlRegisterType<adapters::data_models::LibraryProxyModel>("Librum.models", 1, 0, "LibraryProxyModel"); 将C++类LibraryProxyModel注册为QML类型LibraryProxyModel,可以通过Librum.models.LibraryProxyModel在QML中使用。

  • qRegisterMetaType:将C++类型注册到Qt的元对象系统中,这对于在信号槽机制中传递这些类型的对象是必要的。例如,qRegisterMetaType<adapters::dtos::BookDto>(); 将BookDto类型注册到Qt的元对象系统中。

5.如何在QML中使用这些注册的类型

使用单例类型

假设在QML中使用Style单例类型:

sh 复制代码
import QtQuick 2.15
import Librum.style 1.0

Rectangle {
    width: 200
    height: 200

    color: Style.primaryColor
}

使用注册的C++类型:

假设在QML中使用LibraryProxyModel类型:

sh 复制代码
import QtQuick 2.15
import Librum.models 1.0

ApplicationWindow {
    visible: true
    width: 640
    height: 480

    LibraryProxyModel {
        id: libraryModel
        // 配置模型属性和方法
    }

    ListView {
        model: libraryModel
        // 配置视图属性
    }
}

通过这种方式,你可以在QML文件中利用这些C++类型来构建更复杂的应用程序逻辑和界面。

参考

https://blog.csdn.net/weixin_42208702/article/details/132001997

相关推荐
菜鸟看点7 小时前
自定义Cereal XML输出容器节点
c++·qt
漫步企鹅7 小时前
【蓝牙】Linux Qt4查看已经配对的蓝牙信息
linux·qt·蓝牙·配对
new_zhou8 小时前
Windows qt打包编译好的程序
开发语言·windows·qt·打包程序
看到我,请让我去学习16 小时前
Qt编程-qml操作(js,c++,canvas)
开发语言·qt
哈市雪花17 小时前
相机:Camera原理讲解(使用OpenGL+QT开发三维CAD)
qt·3d·交互·相机·图形学·opengl·视角
津津有味道19 小时前
Qt C++串口SerialPort通讯发送指令读写NFC M1卡
linux·c++·qt·串口通信·serial·m1·nfc
feiyangqingyun21 小时前
全网唯一/Qt结合ffmpeg实现手机端采集摄像头推流到rtsp或rtmp/可切换前置后置摄像头/指定分辨率帧率
qt·智能手机·ffmpeg
随意02321 小时前
Qt 事件
开发语言·qt
鸥梨菌Honevid1 天前
Qt自定义控件(1)——QPaintEvent
开发语言·qt
Mr_Xuhhh2 天前
网络基础(1)
c语言·开发语言·网络·c++·qt·算法