Qt/QML qmlRegisterType 与 setContextProperty(差异) [英] Qt/QML qmlRegisterType vs. setContextProperty (difference)

查看:55
本文介绍了Qt/QML qmlRegisterType 与 setContextProperty(差异)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Qt/QML 应用程序中(此代码通常位于 QtCreator 项目的 main.cpp 中),以下将 C++ 类暴露给 QML 的方式有什么区别:

In Qt/QML application (this code usually resides in main.cpp of QtCreator project), what is the difference between following ways of exposing C++ class to QML:

qmlRegisterType<UePeopleModel>("com.example",
                               1,
                               0,
                               "UePeopleModel");

engine.rootContext()->setContextProperty("uePeopleModel",
                                         uePeopleModel);

?

推荐答案

qmlRegisterType :

有时,QObject 派生类可能需要向 QML 类型系统注册,而不是作为可实例化类型."

"Sometimes a QObject-derived class may need to be registered with the QML type system but not as an instantiable type."

如果您想在一个或多个具有不同属性的 qml 文件中重用 QObject 派生类,请使用 qmlRegisterType.QML 负责初始化这个寄存器类.

Use qmlRegisterType, if you want reuse a QObject-derived class with in one or more than one qml file with different property. QML is responsible for initialization of this register class.

查看此内容以获得更多帮助.从 C++ 定义 QML 类型

See this for more help. Defining QML Types from C++

setContextProperty:

使用 setContextProperty,当您想使用单个全局类访问或从 QML 访问时.这里你需要在使用 setContextProperty() 之前创建这个类对象.

Use setContextProperty, When you want to use a single global class to access to or from QML. Here You need create this class object before use setContextProperty().

注意:由于在 QML 中评估的所有表达式都是在特定上下文中评估的,因此如果修改了上下文,则将重新评估该上下文中的所有绑定.因此,在应用程序初始化之外应谨慎使用上下文属性,因为这可能会导致应用程序性能下降.

Note: Since all expressions evaluated in QML are evaluated in a particular context, if the context is modified, all bindings in that context will be re-evaluated. Thus, context properties should be used with care outside of application initialization, as this may lead to decreased application performance.

查看此内容以获得更多帮助.将 C++ 对象嵌入 QML

See this for more help. Embedding C++ Objects into QML

这篇关于Qt/QML qmlRegisterType 与 setContextProperty(差异)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆