All articles in this series can be viewed here in http://blog.csdn.net/cloud_castle/article/category/2123873
Link to qt5 official demo release set 19 -- Chapter 5: Using list property types
The piechart and pieslice custom qml types we defined earlier are only used for apps. the qml file is used. If you want to use multiple qml types, you can create a scalable plug-in. Since the class defined in C ++ is created as a plug-in for qml, this is slightly different from the plug-in we created in C ++.
1. First, we need to inherit the qqmlextensionplugin class. This is an abstract base class that provides plug-ins for qml loading.
2. Then we need to use the q_plugin_metadata macro in its subclass to register this plug-in to the QT metadata system.
3. Rewrite the pure virtual function registertypes () and use qmlregistertype () to register the qml type in the plug-in. This is the same as what we did in the main function.
4. Then we need to compile a plug-in project file, including template, config, destdir, target, and so on.
5. Finally, we need to create a qmldir file to describe this plug-in.
Take a look at the project directory, where piechart and pieslice have not changed:
This is actually two projects. We can compile the import separately to generate the DLL plug-in, and then put it in a proper place for the app project to use. Let's first look at the files in the import project.
Chartsplugin. h:
# Ifndef chartsplugin_h # define chartsplugin_h //! [0] # include <qqmlextensionplugin> class chartsplugin: Public qqmlextensionplugin // inherits qqmlextensionplugin {q_object q_plugin_metadata (IID "org. qt-project.Qt.QQmlExtensionInterface ") // defines a unique interface for this plug-in and registers it to the Meta Object System Public: void registertypes (const char * URI); // registers the type function overload }; //! [0] # endif
Chartsplugin. cpp:
# Include "chartsplugin. H "//! [0] # include "piechart. H "# include" pieslice. H "# include <qqml. h> void chartsplugin: registertypes (const char * URI) // register the types in the plugin {qmlregistertype <piechart> (Uri, 1, 0, "piechart "); qmlregistertype <pieslice> (Uri, 1, 0, "pieslice ");}//! [0]
We can see that the qml plug-in registration code is still interrupted, but the real implementation lies in the following two files:
The qmldir file has only two sentences:
Module charts // defines the component namespace as charts. This name is also the plugin chartsplugin // definition plug-in used during the last import, consistent with the chartsplugin above.
Next is our import. Pro file:
Template = lib // generate the library file config + = plugin // This library is a plug-in QT + = qml quickdestdir = .. /charts // jump out of the DEBUG directory to the build directory and create the charts directory to store the DLL and qmldir files. Target =$ $ qtlibrarytarget (chartsplugin) headers + = piechart. h pieslice. h chartsplugin. hsources + = piechart. CPP pieslice. CPP chartsplugin. cppdestpath = $ [qt_install_examples]/qml/tutorials/extending/chapter6-plugins/charts // set a variable pointing to the charts directory target. path = $ destpath qmldir. files = $ PWD/qmldirqmldir. path = $ destpathinstils + = target qmldirother_files + = qmldir # copy the qmldir file to the same folder as the plugin binaryqmake_post_link + = $ qmake_copy $ Replace ($ list ($ quote ($ PWD/qmldir) $ destdir),/, $ qmake_dir_sep)
In this way, after this project is compiled, we will find our chartsplugind. dll and qmldir files in the charts folder in the build directory. D indicates that it is generated by debug compilation.
The following APP project calls this plug-in and draws the pie chart:
App. qml:
Import qtquick 2.0 import charts 1.0 // there will be a red wavy line here, and qtcreator will complain that this module may not be found // you can ignore it, you can also set qmlplugindump to let qtquick know the information of this module item {width: 300; Height: 200 piechart {// we can use our custom type anchors like in the previous example. centerin: parent width: 100; Height: 100 slices: [pieslice {anchors. fill: parent color: "red" fromangle: 0; anglespan: 110}, pieslice {anchors. fill: parent color: "black" fromangle: 110; anglespan: 50}, pieslice {anchors. fill: parent color: "blue" fromangle: 160; anglespan: 100}]}
No additional work is required in Main. cpp:
#include <QtQuick/QQuickView>#include <QGuiApplication>int main(int argc, char *argv[]){ QGuiApplication app(argc, argv); QQuickView view; view.setResizeMode(QQuickView::SizeRootObjectToView); view.setSource(QUrl("qrc:///app.qml")); view.show(); return app.exec();}
App. Pro:
Target = chapter6-plugins // set the project name QT + = qml quick # avoid going to debug/release subdirectory # so that our application will see the # import path for the charts module. win32: destdir =. /// this sentence moves the program from debug to the parent build directory, that is, the charts directory at the same level, so that the program can go to the plug-in sources + = Main. cppresources + = app. qrc
Finally there is a root project file chapter6-plugins.pro:
Template = subdirs // compile the sub-directory defined by subdirs below config + = ordered // sequential compilation, which may be less efficient and recommended, however, in this small example, subdirs = import \ // compile the project app in the import directory first. pro // then compile the app Project
Finally, let's take a look at the effect ~