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

From KDE TechBase
(macros that create code belong to .cpp)
 
(6 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{Template:I18n/Language Navigation Bar|Development/Tutorials/Plasma/GettingStarted..SomeMore}}
 


{{TutorialBrowser|
{{TutorialBrowser|
Line 7: Line 7:
name=Creating your first Plasmoid..Some more|
name=Creating your first Plasmoid..Some more|


pre=[http://mindview.net/Books/TICPP/ThinkingInCPP2e.html C++], [http://www.trolltech.com/products/qt/ Qt], [[Getting_Started/Build/KDE4|KDE4 development environment]], [[Development/Tutorials/Plasma/GettingStarted]]|
pre=[http://mindview.net/Books/TICPP/ThinkingInCPP2e.html C++], [http://www.trolltech.com/products/qt/ Qt], [[Getting_Started/Build|KDE development environment]], [[Development/Tutorials/Plasma/GettingStarted]]|


next=|
next=|
Line 27: Line 27:


'''plasma-tutorial1.h'''
'''plasma-tutorial1.h'''
<code cppqt>
<syntaxhighlight lang="cpp-qt">
// These are include guards, we (all over kde) uses them to avoid loading the header multiple times
// These are include guards, we use them (all over kde)
// to avoid loading the header multiple times
#ifndef Tutorial1_HEADER
#ifndef Tutorial1_HEADER
#define Tutorial1_HEADER
#define Tutorial1_HEADER
Line 40: Line 41:
//since C++ won't allow something like:
//since C++ won't allow something like:
// class Plasma::LineEdit;
// class Plasma::LineEdit;
//by itself..
//by itself.
namespace Plasma {
namespace Plasma {
     class LineEdit;
     class LineEdit;
Line 63: Line 64:


#endif
#endif
</code>
</syntaxhighlight>


=== The source file... ===
=== The source file... ===
Line 70: Line 71:


'''plasma-tutorial1.cpp'''
'''plasma-tutorial1.cpp'''
<code cppqt>
<syntaxhighlight lang="cpp-qt">
#include "plasma-tutorial1.h"
#include "plasma-tutorial1.h"


Line 103: 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()
{
{
Line 125: Line 127:


#include "plasma-tutorial1.moc"
#include "plasma-tutorial1.moc"
</code>
</syntaxhighlight>

Latest revision as of 23:27, 11 September 2014


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
#ifndef Tutorial1_HEADER
#define Tutorial1_HEADER

#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;
};

#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

#include "plasma-tutorial1.h"

#include <QPainter>
#include <QFontMetrics>
#include <QSizeF>
#include <QGraphicsLinearLayout>

#include <plasma/theme.h>
#include <plasma/widgets/lineedit.h>
#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)

#include "plasma-tutorial1.moc"