域格MINIPCIE开发板从零调试:AT指令、拨号与踩坑实录
2026/10/15 1:09:28
qmlRegisterType是 Qt QML 中将 C++ 类注册到 QML 系统中的核心函数,它建立了 C++ 类型与 QML 类型系统之间的桥梁。
#include <QQmlApplicationEngine> #include <QQmlContext> #include <QQuickItem> // 注册到默认模块(无命名空间) qmlRegisterType<MyCppClass>("MyModule", 1, 0, "MyClass");template<typename T> int qmlRegisterType(const char *uri, int versionMajor, int versionMinor, const char *qmlName)参数说明:
uri:模块标识符(如 "MyModule")
versionMajor:主版本号
versionMinor:次版本号
qmlName:在 QML 中使用的类型名
// C++ 类定义 class MyItem : public QQuickItem { Q_OBJECT Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged) // ... }; // 在 main.cpp 中注册 #include <QQmlEngine> int main(int argc, char *argv[]) { qmlRegisterType<MyItem>("MyComponents", 1, 0, "MyItem"); // QML 中使用 // import MyComponents 1.0 // MyItem { text: "Hello" } }// 注册到版本 1.0 qmlRegisterType<MyItem>("MyApp", 1, 0, "MyItem"); // 注册到版本 2.0 qmlRegisterType<MyItem>("MyApp", 2, 0, "MyItem");// 1. 标准注册 qmlRegisterType<MyType>("Module", 1, 0, "TypeName"); // 2. 注册不可创建的基类(抽象类) qmlRegisterUncreatableType<BaseType>( "Module", 1, 0, "BaseType", "BaseType is abstract and cannot be instantiated" ); // 3. 注册单例类型 qmlRegisterSingletonType<SingletonType>( "Module", 1, 0, "Singleton", [](QQmlEngine*, QJSEngine*) -> QObject* { return SingletonType::instance(); } ); // 4. 注册附加属性类型 qmlRegisterType<AttachedType>("Module", 1, 0, "AttachedType"); // 5. 注册带附加属性的类型 qmlRegisterType<MainType>("Module", 1, 0, "MainType");class BaseItem : public QQuickItem { Q_OBJECT // 基类注册为不可创建 }; class DerivedItem : public BaseItem { Q_OBJECT // 派生类可创建 }; // 注册 qmlRegisterUncreatableType<BaseItem>( "Components", 1, 0, "BaseItem", "BaseItem is an abstract base class" ); qmlRegisterType<DerivedItem>("Components", 1, 0, "DerivedItem");class MyItem : public QQuickItem { Q_OBJECT public: // 使用静态方法注册 static void registerQmlType() { qmlRegisterType<MyItem>("MyApp", 1, 0, "MyItem"); } }; // 调用 MyItem::registerQmlType();// myitem.h class MyItem : public QQuickItem { Q_OBJECT QML_ELEMENT // 自动注册到同名模块 // ... }; // 在 .pro 文件中添加 CONFIG += qmltypes QML_IMPORT_NAME = MyComponents QML_IMPORT_MAJOR_VERSION = 1// registration.h namespace Registration { void registerTypes() { // 版本 1.0 qmlRegisterType<Button>("MyUI", 1, 0, "Button"); qmlRegisterType<TextField>("MyUI", 1, 0, "TextField"); // 版本 2.0 新增类型 qmlRegisterType<Slider>("MyUI", 2, 0, "Slider"); // 不可创建的基类 qmlRegisterUncreatableType<BaseControl>( "MyUI", 1, 0, "BaseControl", "BaseControl is abstract" ); } } // main.cpp Registration::registerTypes();void registerPlatformSpecificTypes() { qmlRegisterType<PlatformSpecificItem>("MyApp", 1, 0, "PlatformItem"); #ifdef Q_OS_ANDROID qmlRegisterType<AndroidBackButton>("MyApp", 1, 0, "BackButton"); #endif #ifdef Q_OS_IOS qmlRegisterType<IOSStatusBar>("MyApp", 1, 0, "StatusBar"); #endif }// 导入注册的模块 import MyComponents 1.0 // 使用注册的类型 ApplicationWindow { MyItem { id: myItem text: "Hello from C++" } // 使用不同版本的类型 import MyComponents 2.0 Slider { // 版本 2.0 新增的组件 } }// 正确:版本号递增 qmlRegisterType<TypeV1>("Module", 1, 0, "Type"); qmlRegisterType<TypeV2>("Module", 1, 1, "Type"); // 1.1 版本 qmlRegisterType<TypeV3>("Module", 2, 0, "Type"); // 2.0 版本 // QML 中按需导入 import Module 1.0 // 使用 TypeV1 import Module 1.1 // 使用 TypeV2 import Module 2.0 // 使用 TypeV3// 不同模块可以有同名类型 qmlRegisterType<Button>("BasicUI", 1, 0, "Button"); qmlRegisterType<FancyButton>("FancyUI", 1, 0, "Button"); // QML 中使用 import BasicUI 1.0 as Basic import FancyUI 1.0 as Fancy Basic.Button {} // 基本按钮 Fancy.Button {} // fancy 按钮3.注册时机
int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); // 必须在创建引擎前注册类型! qmlRegisterType<MyType>("MyApp", 1, 0, "MyType"); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); return app.exec(); }统一注册点:创建专门的注册函数或类
版本控制:合理规划版本号,向后兼容
模块划分:按功能划分不同模块
错误处理:检查注册返回值
文档注释:为注册的类型添加文档
// 检查注册是否成功 int typeId = qmlRegisterType<MyType>("Module", 1, 0, "Type"); if (typeId == -1) { qWarning() << "Failed to register type"; } // 使用 QML_DECLARE_TYPE 宏 QML_DECLARE_TYPE(MyType)通过qmlRegisterType正确注册 C++ 类型,可以实现强大的 QML/C++ 混合开发,充分利用两种语言的优势。