0

I've got a small application where users may create their own plugins as QML-files. These plugins get notification upon certain events and their user-interfaces are displayed one-by-one in a TabView.

Some plugins however need functionality which can't be provided via QML like writing to files. How would I enable my users to extend the functionality as they please?

Hedge
  • 16,142
  • 42
  • 141
  • 246

1 Answers1

1

Your user must:

  1. Implement a QObject in C++ that provides the required functionality.
  2. Subclass QQmlExtensionPlugin to register that QObject as a QML type.
  3. Build a DLL (or SO or DYLIB) for #1 and #2, and write a qmldir file to tell the QML engine how to load the DLL.

EDIT: Qt Creator automates a bit of this process for you. Go to "File" -> "New File or Project..." -> "Projects" -> "Libraries" -> "Qt Quick 2 Extension Plugin"

You can find more details about this process in the documentation:

Note 1: Your user won't be writing a *.qml file.

Note 2: Each plugin must be in its own subfolder. This is because each C++ plugin needs one qmldir file, but you can't have multiple qmldir files in the same folder.

You can find some real examples in your Qt installation. Go to, say, C:\Qt\5.3\mingw482_32\qml -- all the subfolders here contain QML plugins, mostly written in C++. Here is some sample source code:

JKSH
  • 2,658
  • 15
  • 33
  • Thanks for the precise answer. It helped me a lot. Just one more questions regarding the import path. All user-plugins have their own directory within a folder called plugins. How can I import them all at once? – Hedge Nov 10 '14 at 09:56
  • See the QQmlEngine documentation: http://qt-project.org/doc/qt-5/qqmlengine.html Look for functions like `importPlugin()` and `pluginPathList()` – JKSH Nov 15 '14 at 03:10