C++ API

Compiling advanced C++ widgets with CMake

Compiling With CMake

A template can be found in plasma-framework:

You can run all build and test commands in a single line like so:

CMakeLists.txt

Private C++ QML Plugin

Plasma ships with a number of useful QML plugins like PlasmaCore, PlasmaComponents, PlasmaExtras. Your widget might need more complicated models that interact with C++ libraries or File I/O requiring you to create a QML plugin.

Example Plugins

You can find a template in plasma-framework:

Download KDevelop Template

The mediaframe widget in kdeplasma-addons is a fairly simple example. The plugin has one C++ class to define the plugin, and only defines a single QML Item type.

kdeplasma-addons / applets/mediaframe

While KDE puts `.private` in the namespace of these plugins, they can be accessed by any QML widget / application. If you plan on using someone else's "private" plugin, your widget may experience bugs when Plasma updates.

Another example is the "Kicker" plugin for the "Application Menu" widget which is reused by the kickoff "Application Launcher" widget.

Writing a Plugin

Lets use mediaframe as an example and create our own widget with a plugin.

A full copy of this example can be downloaded as a ZIP or cloned from GitHub. Download ZIP or Git Clone.

To start off, let's work out what we want in the QML code. For this simple example, we will import a new WidgetItem type, which has a property named number and has an invokable function called randomize() which will set the number property to a random number.

package/contents/ui/main.qml

Before moving on to the C++ code, don't forget to create the metadata.json.

package/metadata.json

First new things added to our CMakeLists.txt is listing all our .cpp files that we need to compile. We also define the plugin library name used in the binary filename.

In our CMakeLists.txt, we need to include a few KDE variables so that we compile and install files to the right location. Make sure the folder names match the plugin namespace.

In OpenSUSE, we'll end up installing the following files. Other distros might not use /usr/lib64/qt5 so just use locate qmldir if you are curious where the files are installed to.

Since we're now compiling Qt C++ code, we need to use find_package() to indicate that it's required for compilation. We will need Qt5::Qml to import QQmlExtensionPlugin and use qmlRegisterType(). Since we are sticking to a simple QObject in our new type, we will only need Qt5::Core.

Don't forget to link the components as well.

CMakeLists.txt

The qmldir file is basically the qml plugin metadata file. Since we don't bundle any .qml files in the plugin itself like PlasmaComponents does, this will just define the namespace of the plugin and the plugin library name.

Inside widgetnameplugin.h we extend QQmlExtensionPlugin and indicate we implement the QQmlExtensionInterface which somehow tells it to call registerTypes().

In the .cpp file we register the new QML type. Don't forget to edit the namespace in the assert.

plugin/qmldir

plugin/widgetnameplugin.h

plugin/widgetnameplugin.cpp

Finally we write our new WidgetItem type. In the header file we extend QObject and define the number property. Since we want do not want the number property to be readonly, we define WRITE and a setter function. We also define the numberChanged signal to NOTIFY the GUI when it's modified.

The randomize() method needs Q_INVOKABLE otherwise it cannot be called from QML.

plugin/widgetitem.h

To make development easier, we've imported qDebug() which lets us log to the terminal.

In the setter, we do not emit the signal if the property does not actually change.

plugin/widgetitem.cpp

To compile, install and test this plugin follow the instructions from the previous Compiling With CMake section and the Widget Testing page.

When writing your widget's README.md, you'll want to add uninstall instructions as well.

plasmoid.nativeInterface

The plasmoid.nativeInterface property allows you to directly access C++ objects or functions in the Plasma::Applet instance. You need to extend the Plasma::Applet class first however. The plasmoid.nativeInterface cannot be accessed by another widget namespace, so this code is private.

See the SystemTray container for an example.

CMakeLists.txt

systemtraycontainer.cpp

Containment (SystemTray, Panel, Grouping)

Note:

  • The Grouping widget only displays one child widget (aka Applet) at a time.

  • The SystemTray can display multiple CompactRepresentations at a time, but only one FullRepresentation in the main popup.

  • The Panel can display Compact or Full representations next to each other but is the most complicated codebase to read.

Examples:

Translate C++ Strings

If you want to messages translated in your C++ code, you will need to import KF5::I18n and define the translation domain in your CMakeLists.txt.

Last updated