Thursday, 2 January 2014

Develop Qml Plugin 2

Let's see my plugin project folder.
/plugin$ tree
├── imports
│   └── demo
│       ├── hello.qml
│       └── qmldir
├── plugin.cpp
└── test.qml
The plugin project name is called hello, is its project file. All files are under plugin folder, you are free to change the plugin folder to any name you like.
I have introduced test.qml file in previous article, it's just a test qml file, not the part of plugin.
plugin.cpp file is a C++ implementation file, it contains a exposed C++ class.
Look inside the file imports/demo/hello.qml, all qml objects in this file is used as Hello class in test.qml. Actually, there is no name defined in hello.qml file, it is defined in imports/demo/qmldir file, I will talk about qmldir file later.
The content of hello.qml is shown below:
import QtQuick 2.0
Rectangle {
    width: 200; height: 200; color: "gray"
    Text {
        font.bold: true;
        font.pixelSize: 14;
        color: "white"
        anchors.horizontalCenter: parent.horizontalCenter
It's just a normal qml file that includes a rectangle.

qmldir file is very important, see its content now:
module demo
Hello 1.0 hello.qml
plugin hello
 The first line defines module name called demo. Note, the module name can be separated by dot operation. Rule is the combination of import path and domain name will point to the qmldir file's location.
In my case, the import file path is relative path ./imports under plugin folder. Plus the demo path, the real path would be ./imports/demo/, it is right, so qmldir file can be located correctly.
To get more information please refer to official manual:
The second line define a Hello class which comes from hello.qml. In test.qml file, I used this name.
In the third line, hello means the C++ plugin lib file name, e.g. on Ubuntu, it should be, on Windows, it would be hello.dll. Do not treat it as the exposed C++ class name from plugin lib, because we can expose more than one C++ classes from one plugin lib file.

As a summary, I draw a diagram for plugin architecture.

No comments: