Thursday 2 January 2014

Develop Qml Plugin 3

Have a look at hello.pro file now.
TEMPLATE = lib
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:
http://qt-project.org/doc/qt-5/qmake-project-files.html
http://qt-project.org/doc/qt-5/qmake-language.html

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>
//![0]
class ClickHandler : public QObject
{
  Q_OBJECT
  Q_PROPERTY(QString value READ value)
  //![0]
public:
  ClickHandler(QObject *parent=0) : QObject(parent)
  {
  }
  ~ClickHandler()
  {
  }
  QString value() const {
    return "hello, world";
  }
};
//![plugin]
class QExampleQmlPlugin : public QQmlExtensionPlugin
{
  Q_OBJECT
  Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface")
  public:
  void registerTypes(const char *uri)
  {
    qmlRegisterType<ClickHandler>(uri, 1, 0, "MyClick");
  }
};
//![plugin]
#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.
http://doc-snapshot.qt-project.org/qdoc/qtqml-cppintegration-definetypes.html
http://doc-snapshot.qt-project.org/qdoc/qml-extending-tutorial-index.html
http://doc-snapshot.qt-project.org/qdoc/qtqml-cppintegration-exposecppattributes.html
http://doc-snapshot.qt-project.org/qdoc/qqmlextensionplugin.html

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 libhello.so
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 libhello.so plugin.o  -L/home/dean/Qt5.2.0/5.2.0/gcc_64/lib -lQt5Qml -lQt5Network -lQt5Gui -lQt5Core -lGL -lpthread
mv -f libhello.so 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.
http://qt-project.org/doc/qt-5/metaobjects.html

No comments:

Followers

Contributors