qmlRegisterType 注册C++类型出现 module not fount

使用 qmlRegisterSingletonType 或 qmlRegisterType 想QML注册C++类,按照使用文档上方法添加如下:

    qmlRegisterSingletonType<CProtoInfoModel>("LdpModel", 1, 0, "protoInfoModel", CProtoInfoModel::singletonInstance);
    qmlRegisterType<CStateMoniterModel>("LdpModel", 1, 0, "stateMoniterModel");

然后再QML文件中添加如下import

import LdpModel 1.0

出现如下错误:

qrc:/qml/LdpProto.qml:31 Type StateMonitor unavailable
qrc:/qml/StateMonitor.qml:7 module "LdpModel" is not installed

在网上找了很久资料,发现都是这么用的,但就是不正确,很是无语。

最后在一片文章中看到这样一句:注意:第四个QML的类名首字母一定要大写,要不然会报错。。而且是那种你找不到的

然后修改为:

    qmlRegisterSingletonType<CProtoInfoModel>("LdpModel", 1, 0, "ProtoInfoModel", CProtoInfoModel::singletonInstance);
    qmlRegisterType<CStateMoniterModel>("LdpModel", 1, 0, "StateMoniterModel");

最后一切OK,然后看了下帮助文档,别人第一句就是 This template function registers the C++ type in the QML system with the name qmlName,意思是注册一个类型到QML系统里面去,既然是用到QML里面的类型,这里第四个参数代表QML系统里面使用的类型名,所以首字母就必须大写

这个说明一点:一定要理解帮助文档。

 

posted @ 2022-09-21 11:21  菜鸟_IceLee  阅读(815)  评论(1编辑  收藏  举报