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 @   菜鸟_IceLee  阅读(1047)  评论(1编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示