QML 我们如何与C++一起使用,有一部份内容保持英文,方便理解 我们知道 ,通过Qt Declarative module , C++可以动态创建和操纵QML 的组件( cpmponents) ,我们利用这些API 使用C++来拓展我们的QML 程序,反过来也可以将QML嵌入到你的C++程序中。通过Qt 的元对象系统( Qt‘s metaobject system),我们可以利用 Qt 中的信号与槽机制使QML 和 Qt objects 相互通信( communicate)。另外,QML plugins可以用来创建可复用的QML 组件。 使用C++和 QML 混合编程可能出于以下原因: You may want to mix QML and C++ for a number of reasons. For example: To use functionality defined in a C++ source (for example, when using a C++ Qt-based data model, or calling functions in a third-party C++ library) To access functionality in the Qt Declarative module (for example, to dynamically generate images usingQDeclarativeImageProvider) To write your own QML elements (whether for your applications, or for distribution to others 首先我们介绍下Qt Declarative module 中几个核心的类: QDeclarativeEngine: QML 的执行环境,负责解释执行QML 代码。每一个程序都应至少包含一个engine 实例 ; QDeclarativeComponent:创建一个封装了QML 文件的组件; QDeclaraContext:提供了一个允许C++程序将数据暴露给由QDeclaraEngine 创建的 QML 组件的上下文环境; 例如: QDeclarativeEngine engine; QDeclarativeComponent component(&engine, QUrl::fromLocalFile("MyRectangle.qml")); QObject *rectangleInstance = component.create(); delete rectangleInstance; QObject * QDeclarativeComponent::create (QDeclarativeContext * context = 0) [virtual] QML 我们如何与C++一起使用 我们如何通过C++拓展我们的QML 呢? 1.我们可以通过C++动态创建一个QML 组件并且我们能够对它进行一些操作 2.我们可以使一个C++对象(比如:继承自QObject 的类的对象)和他的属性作为一个QML的组件 3.定义一个QML 组件 Loading QML Components from C++ QDeclarativeComponent 将一个QML 组件(文件)装载成一个C++ Object(对象)。使用QDeclarativeComponent 需要调用QDeclarativ...