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系统里面使用的类型名,所以首字母就必须大写。
这个说明一点:一定要理解帮助文档。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现