The QDeclarativeExtensionPlugin class provides an abstract base for custom QML extension plugins. 更多...
| 头: | #include <QDeclarativeExtensionPlugin> |
| Since: | Qt 4.7 |
| 继承: | QObject |
| QDeclarativeExtensionPlugin (QObject * parent = 0) |
| virtual void | initializeEngine (QDeclarativeEngine * engine , const char * uri ) |
| virtual void | registerTypes (const char * uri ) = 0 |
The QDeclarativeExtensionPlugin class provides an abstract base for custom QML extension plugins.
QDeclarativeExtensionPlugin is a plugin interface that makes it possible to create QML extensions that can be loaded dynamically into QML applications. These extensions allow custom QML types to be made available to the QML engine.
To write a QML extension plugin:
QML extension plugins can be used to provide either application-specific or library-like plugins. Library plugins should limit themselves to registering types, as any manipulation of the engine's root context may cause conflicts or other issues in the library user's code.
Suppose there is a new
TimeModel
C++ class that should be made available as a new QML element. It provides the current time through
hour
and
minute
properties, like this:
class TimeModel : public QObject { Q_OBJECT Q_PROPERTY(int hour READ hour NOTIFY timeChanged) Q_PROPERTY(int minute READ minute NOTIFY timeChanged) ...
To make this class available as a QML type, create a plugin that registers this type with a specific
模块
使用
qmlRegisterType
(). For this example the plugin module will be named
com.nokia.TimeExample
(as defined in the project file further below).
class QExampleQmlPlugin : public QDeclarativeExtensionPlugin { Q_OBJECT public: void registerTypes(const char *uri) { Q_ASSERT(uri == QLatin1String("org.qtproject.TimeExample")); qmlRegisterType<TimeModel>(uri, 1, 0, "Time"); } }; Q_EXPORT_PLUGIN2(qmlqtimeexampleplugin, QExampleQmlPlugin);
This registers the
TimeModel
class with the 1.0 version of this plugin library, as a QML type called
时间
。
Q_ASSERT
statement ensures the module is imported correctly by any QML components that use this plugin.
The project file defines the project as a plugin library and specifies it should be built into the
com/nokia/TimeExample
目录:
TEMPLATE = lib CONFIG += qt plugin QT += declarative DESTDIR = com/nokia/TimeExample TARGET = qmlqtimeexampleplugin ...
Finally, a
qmldir 文件
is required in the
com/nokia/TimeExample
directory that describes the plugin. This directory includes a
Clock.qml
file that should be bundled with the plugin, so it needs to be specified in the
qmldir
文件:
Once the project is built and installed, the new
时间
element can be used by any QML component that imports the
com.nokia.TimeExample
模块:
import org.qtproject.TimeExample 1.0 // import types from the plugin Clock { // this class is defined in QML (org/qtproject/TimeExample/Clock.qml) Time { // this class is defined in C++ (plugin.cpp) id: time } hours: time.hour minutes: time.minute }
The full source code is available in the plugins example .
The 教程:采用 C++ 编写 QML 扩展 also contains a chapter on creating QML plugins.
另请参阅 QDeclarativeEngine::importPlugin () 和 如何创建 Qt 插件 .
Constructs a QML extension plugin with the given parent .
Note that this constructor is invoked automatically by the Q_EXPORT_PLUGIN2 () macro, so there is no need for calling it explicitly.
[虚拟]
void
QDeclarativeExtensionPlugin::
initializeEngine
(
QDeclarativeEngine
*
engine
, const
char
*
uri
)
Initializes the extension from the uri 使用 engine . Here an application plugin might, for example, expose some data or objects to QML, as context properties on the engine's root context.
[pure virtual]
void
QDeclarativeExtensionPlugin::
registerTypes
(const
char
*
uri
)
Registers the QML types in the given uri . Subclasses should implement this to call qmlRegisterType () for all types which are provided by the extension plugin.
The uri is an identifier for the plugin generated by the QML engine based on the name and path of the extension's plugin library.