Chapter 6: Writing an Extension Plugin
Currently the PieChart
and PieSlice
types are used by app.qml
, which is displayed using a QDeclarativeView in a C++ application. An alternative way to use our QML extension is to create a plugin library to make it available to the QML engine. This allows app.qml
to be loaded with the QML Viewer (or some other QML runtime application) instead of writing a main.cpp
file and loading our own C++ application.
To create a plugin library, we need:
- A plugin class that registers our QML types
- A project file that describes the plugin
- A qmldir file that tells the QML engine to load the plugin
First, we create a plugin class named ChartsPlugin
. It subclasses QDeclarativeExtensionPlugin and registers our QML types in the inherited registerTypes() method. It also calls Q_EXPORT_PLUGIN2 for Qt's plugin system.
Here is the ChartsPlugin
definition in chartsplugin.h
:
And its implementation in chartsplugin.cpp
:
Then, we write a .pro
project file that defines the project as a plugin library and specifies with DESTDIR that library files should be built into a "lib" subdirectory:
Finally, we add a qmldir file that is automatically parsed by the QML engine. In this file, we specify that a plugin named "chapter6-plugin" (the name of the example project) can be found in the "lib" subdirectory:
Now we have a plugin, and instead of having a main.cpp and an executable, we can build the project and then load the QML file in the QML Viewer:
qmlviewer app.qml
(On Mac OS X, you can launch the "QMLViewer" application instead.)
Notice the "import Charts 1.0" statement has disappeared from app.qml
. This is because the qmldir
file is in the same directory as app.qml
: this is equivalent to having PieChart.qml and PieSlice.qml files inside the project directory, which could both be used by app.qml
without import statements.