Creating your first Plasmoid
Tutorial Series   Plasma Tutorial
Previous   C++, Qt, KDE4 development environment
What's Next  
Further Reading   CMake


We're going to start creating a simple plasmoid in this tutorial, to keep things simple we will only create a static plasmoid, it will contain the following items.

  • SVG Background
  • Icon
  • Some nice text

I seem to be unable to upload images to the wiki, example image

The Code

The .desktop file

Every Plasmoid needs a .desktop file to tell plasma how they should be started, and what name they carry.

plasma-applet-tutorial1.desktop [Desktop Entry] Encoding=UTF-8 Name=Tutorial 1 Comment=Plasma Tutorial 1 Type=Service ServiceTypes=Plasma/Applet

X-KDE-Library=plasma_applet_tutorial1 X-KDE-PluginInfo-Author=Bas Grolleman [email protected] X-KDE-PluginInfo-Name=plasma_applet_tutorial1 X-KDE-PluginInfo-Version=0.1 X-KDE-PluginInfo-Website= X-KDE-PluginInfo-Category= X-KDE-PluginInfo-Depends= X-KDE-PluginInfo-License=GPL X-KDE-PluginInfo-EnabledByDefault=true

The most important bits are the X-KDE-Library and X-KDE-PluginInfo-Name, they are the paste between your class and plasma, without it, nothing will start.

The header file

This is the example header file, I will add lot's of comment's in the code to explain everything.

plasma-tutorial1.h // Here we avoid loading the header multiple times

  1. ifndef Tutorial1_HEADER
  2. define Tutorial1_HEADER

// We need the Plasma Applet headers

  1. include <Plasma/Applet>
  2. include <Plasma/Svg>
  3. include <KIcon>

// Define our plasma Applet class PlasmaTutorial1 : public Plasma::Applet {

       // Basic Create/Destroy
       PlasmaTutorial1(QObject *parent, const QStringList &args);
       // This function returns the size of your applet
       QRectF boundingRect() const;
       // The paintInterface procedure paints the applet to screen
       void paintInterface(QPainter *painter,�
               const QStyleOptionGraphicsItem *option,
               QWidget *widget =0);
       Plasma::Svg* m_tutorial_theme;
       KIcon* m_tutorial_icon;


// This is the command that links your applet to the .desktop file K_EXPORT_PLASMA_APPLET(tutorial1, PlasmaTutorial1)

  1. endif

QRectF boundingRect()

The boundingRect() function tells plasma the actual size of the plasmoid, this is important because we need to know how much space is taken on screen.

If you have issues with your plasmoid leaving pixels behind when dragging this is almost always a result of a incorrect boudingRect()

void paintInterface(...)

You could call this the main function, since this paints the plasmoid on screen, here you define how you want your plasmoid to look.

K_EXPORT_PLASMA_APPLET ( <name>, <class> )

This is a small but very important part, this links your classname to the applet name in the .desktop file, if your applet doesn't seem to be loaded than chances are there is a difference between this and your .desktop file

The K_EXPORT_PLASMA_APPLET adds "plasma_applet_", please pay attention to this when setting up your .desktop file to avoid a name difference

The actual work file

Here is the body of the function, again with a lot of comments in between.


  1. include "plasma-tutorial1.h"
  2. include <QPainter>
  3. include <QFontMetrics>
  4. include <KIcon>
  5. include <Plasma/Svg>

PlasmaTutorial1::PlasmaTutorial1(QObject *parent,

                                const QStringList &args)
   : Plasma::Applet(parent, args)

{ }

PlasmaTutorial1::~PlasmaTutorial1() { }

QRectF PlasmaTutorial1::boundingRect() const {

   // In this tutorial we use a fixed size
   return QRectF(0,0,128,128);


void PlasmaTutorial1::paintInterface(QPainter *p,

   const QStyleOptionGraphicsItem *option, QWidget *widget)


   // First we create the objects to hold the icon
   // and background image
   KIcon icon("document");
   Plasma::Svg* bg =
       new Plasma::Svg("widgets/background",this);
   // Tell the Plasma/Svg object to use the whole image, 
   // instead of parts of it.
   // Now we draw the applet, starting with the background
   // The we place the icon and text
       "Hello Plasmoid!");


  1. include "plasma-tutorial1.moc"


As you can see in the example code where using the Plasma::Svg object, there are some important things to note here. First we're using a relative path widgets/background, trying to do this as often as possible as it will make plasma skinable and make the plasmoids look like a combined whole instead of a group of separate unrelated applications.

Second is something we didn't use here, Plasma/Svg can draw a subset of an svg file, this is usefull since we only require a single .svg file to hold all images. For a good example I would suggest opening the clock .svg, you will see that it has a background, 3 handles (hour, minute and seconds) and a foreground (the glass). Now because of the ability to put this all in one file the .svg file shows a clock, and this is much nicer than 5 separate files that you have to imagine on top of eachother.

Content is available under Creative Commons License SA 4.0 unless otherwise noted.