Thursday, 2 January 2014

Develop Qml Plugin 3

Have a look at file now.
CONFIG += plugin
QT += qml
DESTDIR = imports/demo
TARGET  = hello
SOURCES += plugin.cpp
qml.files = test.qml
qml.path += ./
pluginfiles.files += \
    imports/demo/qmldir \
    imports/demo/hello.qml \
pluginfiles.path += imports/demo
target.path += imports/demo
INSTALLS += target qml pluginfiles
 It's a qmake project file. The official docs are located at:

qmake reads this pro file and generates Makefile, before running qmake command, let's see how to implement an exposed C++ class in plugin.cpp file.
#include <QtQml/QQmlExtensionPlugin>
#include <QtQml/qqml.h>
#include <qdebug.h>
#include <qdatetime.h>
#include <qbasictimer.h>
#include <qcoreapplication.h>
class ClickHandler : public QObject
  Q_PROPERTY(QString value READ value)
  ClickHandler(QObject *parent=0) : QObject(parent)
  QString value() const {
    return "hello, world";
class QExampleQmlPlugin : public QQmlExtensionPlugin
  Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface")
  void registerTypes(const char *uri)
    qmlRegisterType<ClickHandler>(uri, 1, 0, "MyClick");
#include "plugin.moc"
To implement this, need the following steps:
1. Define a class which inherits from QQmlExtensionPlugin and use above Q_ macros
2. Define a class that you want to expose, here I have ClickHandler class, then register its type in the above class's registerTypes method, also with the version and short name.
3. Do not forget the plugin.moc
The above code is not long, but still contains many detail information. Please refer to official doc to get more information.

Now, run commands to build this project.

dean@sloop2:~/test/plugin$ qmake
dean@sloop2:~/test/plugin$ make
/home/dean/Qt5.2.0/5.2.0/gcc_64/bin/moc -DQT_NO_DEBUG -DQT_PLUGIN -DQT_QML_LIB -DQT_NETWORK_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I../../Qt5.2.0/5.2.0/gcc_64/mkspecs/linux-g++ -I. -I../../Qt5.2.0/5.2.0/gcc_64/include -I../../Qt5.2.0/5.2.0/gcc_64/include/QtQml -I../../Qt5.2.0/5.2.0/gcc_64/include/QtNetwork -I../../Qt5.2.0/5.2.0/gcc_64/include/QtGui -I../../Qt5.2.0/5.2.0/gcc_64/include/QtCore -I. plugin.cpp -o plugin.moc
g++ -c -pipe -O2 -Wall -W -D_REENTRANT -fPIC -DQT_NO_DEBUG -DQT_PLUGIN -DQT_QML_LIB -DQT_NETWORK_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I../../Qt5.2.0/5.2.0/gcc_64/mkspecs/linux-g++ -I. -I../../Qt5.2.0/5.2.0/gcc_64/include -I../../Qt5.2.0/5.2.0/gcc_64/include/QtQml -I../../Qt5.2.0/5.2.0/gcc_64/include/QtNetwork -I../../Qt5.2.0/5.2.0/gcc_64/include/QtGui -I../../Qt5.2.0/5.2.0/gcc_64/include/QtCore -I. -o plugin.o plugin.cpp
rm -f
g++ -Wl,-O1 -Wl,-rpath,/home/dean/Qt5.2.0/5.2.0/gcc_64 -Wl,-rpath,/home/dean/Qt5.2.0/5.2.0/gcc_64/lib -shared -o plugin.o  -L/home/dean/Qt5.2.0/5.2.0/gcc_64/lib -lQt5Qml -lQt5Network -lQt5Gui -lQt5Core -lGL -lpthread
mv -f imports/demo/ 
From above output, you will see qmake use moc coming from Qt5.2.0 to create plugin.moc from plugin.cpp file. To know moc, you need to learn something about Qt Meta-Object System.

No comments: