Writing native C++/Qt plugins

A native plugin is a Qt Plugin, i.e. a shared library providing a particular interface.

CMake

Having a standardized plugin project structure the albert_plugin macro takes care of most of the CMake boilerplate code. Read its documentation in the header of the CMake module before you proceed. This is the standard plugin directory structure of a plugin:

─┬─  my_plugin      
 ├──  CMakeLists.txt      
 ├──  metadata.json
 ├─┬─  src    
 │ └──  …
 └─┬─  i18n        
   └──  …       

A basic metadata file looks like this (See also the metadata.json files of the official plugins):

{
    "name": "My Plugin",
    "description": "Do useful stuff",
    "authors": ["@myname"],
    "license": "MIT",
    "url": "https://github.com/myusername/my-albert-plugin",
}

A minimal working CMakeLists.txt (See also the CMakeLists.txt files of the official plugins):

cmake_minimum_required(VERSION 3.16)
project(my_plugin VERSION 1.0)
find_package(Albert REQUIRED)
albert_plugin()

C++

A Qt plugin class has to inherit QObject, contain the Q_OBJECT macro, specify an interface identifier and the metadata file path. The relevant base classes and the ALBERT_PLUGIN macro takes care of this.

Albert plugins have to inherit the PluginInstance class. Usually you dont want to subclass PluginInstance directly but rather ExtensionPlugin which implements the Extension interface using the metadata of the plugin instance.

A basic plugin looks like this (See also the plugin header files of the official plugins):

#pragma once
#include <albert/extensionplugin.h>

class Plugin : public albert::ExtensionPlugin
{
    ALBERT_PLUGIN
};

What’s next?

From here on it depends on the interface you want to implement.