Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [Solved] C++ plugin for QML
Forum Updated to NodeBB v4.3 + New Features

[Solved] C++ plugin for QML

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 1 Posters 2.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    imihajlov
    wrote on last edited by
    #1

    Hi, everybody! I have an application that consists of a QML part and C++ part (a lot of classes that can be accessed from QML). The QML files are located in a qrc.
    I want to detach the C++ part and make a shared (or static) library named 'engine' to use with other projects.
    I have read some tutorials and detached the C++ files. Also I've added a file "plugin.cpp" with the following code:
    @#include <QtPlugin>
    #include <QDeclarativeExtensionPlugin>
    #include "version.h"
    #include "headers.h"

    class Plugin: public QDeclarativeExtensionPlugin
    {
    Q_OBJECT
    public:
    void registerTypes(const char *uri)
    {
    #include "regtypes.inc"
    }
    };

    Q_EXPORT_PLUGIN2(engine, Plugin)@

    headers.h and regtypes.inc are autogenerated files that do the registration. The example line of regtypes.inc:
    @qmlRegisterType<Printer>(uri, VERSION_MAJOR, VERSION_MINOR, "Printer");@

    The library compiles OK, I get the file libengine.so.

    Please help me with importing the plugin into the QML program! I've tried many different ways, but I didn't succeed.
    Where should I put the shared library? Should I link my executable with the library? Should I write the qmldir file and where should I put it? Should I do addImportPath()?

    1 Reply Last reply
    0
    • I Offline
      I Offline
      imihajlov
      wrote on last edited by
      #2

      I've succeeded in importing my plugin from qmlviewer. To do that I had to add header file plugin.h with the definition of class Plugin to let the MOC process the class. Without that step the qmlviewer was unable to load the shared library.
      The directory structure is:
      @main.qml
      engine
      engine/qmldir
      engine/libengine.so@

      qmldir:
      @plugin engine@

      main.qml:
      @import QtQuitck 1.1
      import engine 0.1

      Rectangle {
      color: "red"
      Printer {
      id: printer
      }
      }
      @

      The command to run qmlviewer is:
      @qmlviewer -I . main.qml@

      Now I'm going to try importing the plugin from my executable.

      1 Reply Last reply
      0
      • I Offline
        I Offline
        imihajlov
        wrote on last edited by
        #3

        To do importing the plugin from my application I had to add import path to the main view:
        @mv->engine()->addImportPath("."); // mv is an object of class derived from QDeclarativeView@

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved