Development/Tutorials/Plasma4/GettingStarted..Some More: Difference between revisions

From KDE TechBase
Line 104: Line 104:


//we do all of our GUI stuff in here. Only basic things should be placed
//we do all of our GUI stuff in here. Only basic things should be placed
//in the regular constructor, like turning on the configuration interface, resize of main widget, simple stuff.
//in the regular constructor, like turning on the configuration interface,
//resize of main widget, simple stuff.
void PlasmaTutorial1::init()
void PlasmaTutorial1::init()
{
{

Revision as of 12:16, 12 June 2011


Development/Tutorials/Plasma/GettingStarted..SomeMore


Creating your first Plasmoid..Some more
Tutorial Series   Plasma Tutorial
Previous   C++, Qt, KDE development environment, Development/Tutorials/Plasma/GettingStarted
What's Next  
Further Reading   CMake

Abstract

For the most part, I will skip the prelude and assume that you already know how to make the CMake file, to build, and to test your Plasmoid. If you need to refer back to the previous tutorial, feel free. This tutorial is covering some more material, most of it's pretty basic but hey, that's what tutorials are for ;-)

As of the time of this writing, the Plasma constructors for widgets tend to be from what I see (in general), lesser than their Qt base classes. This kind of threw me off, like pushButton->setIcon (I think).

Feel free to add anything you want, there's certainly not enough here :'(

I left it named "Tutorial1" just because it's easier/no point for it to be otherwise/I'm lazy.

The header file

This is the example header file. Comments have been added in the code for clarity.

plasma-tutorial1.h // These are include guards, we use them (all over kde) // to avoid loading the header multiple times

  1. ifndef Tutorial1_HEADER
  2. define Tutorial1_HEADER
  1. include <Plasma/Applet>


class QSizeF;

//this is how classes in Plasma can be forward-declared //since C++ won't allow something like: // class Plasma::LineEdit; //by itself. namespace Plasma {

   class LineEdit;
   class PushButton;

}

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

   Q_OBJECT
   public:
       // Basic Create/Destroy
       PlasmaTutorial1(QObject *parent, const QVariantList &args);
       ~PlasmaTutorial1();
       void init();
   private:
       Plasma::LineEdit *m_lineEdit;
       Plasma::PushButton *m_pushButton;

};

  1. endif

The source file...

Remember, Plasma has camel-cased header includes as well, so if you prefer, you can just use e.g. #include <Plasma/Svg>, etc.

plasma-tutorial1.cpp

  1. include "plasma-tutorial1.h"
  1. include <QPainter>
  2. include <QFontMetrics>
  3. include <QSizeF>
  4. include <QGraphicsLinearLayout>
  1. include <plasma/theme.h>
  2. include <plasma/widgets/lineedit.h>
  3. include <plasma/widgets/pushbutton.h>


PlasmaTutorial1::PlasmaTutorial1(QObject *parent, const QVariantList &args)

   : Plasma::Applet(parent, args),
     m_lineEdit(0), m_pushButton(0)  //don't forget to initialze your pointers to zero or *else*!!

{

   // this will get us the standard applet background, for free!
   setBackgroundHints(DefaultBackground);
   resize(200, 200);

}


PlasmaTutorial1::~PlasmaTutorial1() {

   if (hasFailedToLaunch()) {
       // Do some cleanup here
   } else {
       // Save settings
   }

}

//we do all of our GUI stuff in here. Only basic things should be placed //in the regular constructor, like turning on the configuration interface, //resize of main widget, simple stuff. void PlasmaTutorial1::init() {

 QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(this);
 layout->setOrientation(Qt::Vertical); //so widgets will be stacked up/down
 m_lineEdit = new Plasma::LineEdit(this);
 m_lineEdit->setText("Hey! This is a Plasma line edit.");
 m_pushButton = new Plasma::PushButton(this);
 m_pushButton->setText("Whoa! This is a Plasma pushbutton.");
 layout->addItem(m_lineEdit);
 layout->addItem(m_pushButton);

}

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

  1. include "plasma-tutorial1.moc"