QDeclarativeExtensionPlugin Class Reference
[QtDeclarative module]
该QDeclarativeExtensionPlugin类提供了一个抽象基自定义QML扩展插件。More…
继承QObject。
Methods
__init__ (self, QObject parent = None)
initializeEngine (self, QDeclarativeEngine engine, str uri)
registerTypes (self, str uri)
Detailed Description
该QDeclarativeExtensionPlugin类提供了一个抽象基自定义QML扩展插件。
QDeclarativeExtensionPlugin是一个插件接口,使得它可以创建能够动态地加载到QML应用QML扩展。这些扩展允许自定义的QML类型被提供给QML引擎。
写一个QML扩展插件:
- Subclass QDeclarativeExtensionPlugin, implement registerTypes() method to register types using qmlRegisterType(), and export the class using the Q_EXPORT_PLUGIN2() macro
- Write an appropriate project file for the plugin
- Create a qmldir file to describe the plugin
QML扩展插件可用于提供任何特定应用程序或库类插件。图书馆插件应该限制自己注册的类型,因为任何操作引擎的根上下文可能会导致在库用户的代码冲突或其他问题。
An example
假设有一个新TimeModel
C + +类应该提供一个新的QML元素。它提供了在当前时间通过hour
和minute
属性,像这样:
class TimeModel : public [QObject]($docs-qobject.html)
{
Q_OBJECT
Q_PROPERTY(int hour READ hour NOTIFY timeChanged)
Q_PROPERTY(int minute READ minute NOTIFY timeChanged)
...
为了使这个类可以作为一个QML类型,创建一个插件,注册这个类型与特定的module using qmlRegisterType( ) 。在这个例子中的插件模块将被命名为com.nokia.TimeExample
(如在下面进一步在项目文件中定义)。
class QExampleQmlPlugin : public QDeclarativeExtensionPlugin
{
Q_OBJECT
public:
void registerTypes(const char *uri)
{
Q_ASSERT(uri == QLatin1String("com.nokia.TimeExample"));
qmlRegisterType<TimeModel>(uri, 1, 0, "Time");
}
};
Q_EXPORT_PLUGIN2(qmlqtimeexampleplugin, QExampleQmlPlugin);
这将注册TimeModel
级与1.0版本的插件库,作为所谓的QML类型Time
。该Q_ASSERT语句确保该模块是通过使用这个插件的任何QML组件正确导入。
项目文件中的项目定义为一个插件库,并指定它应该被内置到com/nokia/TimeExample
目录:
TEMPLATE = lib
CONFIG += qt plugin
QT += declarative
DESTDIR = com/nokia/TimeExample
TARGET = qmlqtimeexampleplugin
...
最后,一个qmldir file是需要在com/nokia/TimeExample
目录描述插件。此目录包含一个Clock.qml
应该与插件捆绑在一起,因此它需要在指定的文件qmldir
文件:
Clock 1.0 Clock.qml
plugin qmlqtimeexampleplugin
一旦项目建成并安装新的Time
元素可以使用一个导入的任何QML组件com.nokia.TimeExample
模块:
import com.nokia.TimeExample 1.0 // import types from the plugin
Clock { // this class is defined in QML (com/nokia/TimeExample/Clock.qml)
Time { // this class is defined in C++ (plugin.cpp)
id: time
}
hours: time.hour
minutes: time.minute
}
完整的源代码是可用plugins example。
该Tutorial: Writing QML extensions with C++还包含有关创建QML插件一章。
Method Documentation
QDeclarativeExtensionPlugin.__init__ (self, QObject parent = None)
该parent的说法,如果不是没有,原因self通过Qt的,而不是PyQt的拥有。
构造一个QML扩展插件与给定parent。
注意,此构造是由自动调用Q_EXPORT_PLUGIN2()宏,所以没有必要显式地调用它。
QDeclarativeExtensionPlugin.initializeEngine (self, QDeclarativeEngine engine, str uri)
初始化扩展从uri使用engine。在这里,一个应用程序的插件可能,例如,暴露一些数据或对象,以QML ,作为发动机的根上下文上下文属性。
QDeclarativeExtensionPlugin.registerTypes (self, str uri)
这种方法是抽象的,应在任何子类中重新实现。
寄存器的QML类型在给定的uri。子类应该实现这个调用qmlRegisterType()为这是由扩展插件提供的所有类型。
该uri是一个标识符的基础上扩展的插件库的名称和路径由QML引擎生成的插件。