My question is , can my interface inherit from QObject and how to do that ? Well , i know that interfaces in C++ are simply classes that contains only virtual methods , and normally a class can inherit from a superclass. But if i do so i get an error due to ambiguous QObject references. I need to inherit QObject to add signals /slot feature to my plugins.
My interface
#ifndef LABELINTERFACE_H
#define LABELINTERFACE_H
#include <QLabel>
#include <QObject>
class LabelInterface : public QObject {
public :
virtual ~LabelInterface() {}
virtual QLabel* newLabel() = 0;
public slots:
virtual void setLabelText() = 0;
};
Q_DECLARE_INTERFACE (LabelInterface,"com.stefan.Plugin.LabelInterface/1.0")
#endif // LABELINTERFACE_H
Plugin header file
#ifndef LABELPLUGIN_H
#define LABELPLUGIN_H
#include "labelinterface.h"
class LabelPlugin : public LabelInterface
{
Q_OBJECT
Q_INTERFACES(LabelInterface)
public:
QLabel* label;
QLabel* newLabel();
LabelPlugin() {}
~LabelPlugin() {}
public slots:
void setTextForLabel();
};
#endif // LABELPLUGIN_H
Implementation file
#include <QtGui>
#include "labelplugin.h"
QLabel* LabelPlugin::newLabel() {
label = new QLabel("This plugin works");
return label;
}
void LabelPlugin::setTextForLabel() {
label->setText("This plugin works fine");
}
// Exporta plugin-ul
Q_EXPORT_PLUGIN2 (labelplugin,LabelPlugin)
I get error
labelplugin.cpp:18: error: cannot allocate an object of abstract type ‘LabelPlugin’
You forgot to implement
virtual void setLabelText() = 0;
You implemented
void setTextForLabel();
was that a typo? To instantiate a class you need to override and implement all pure virtual methods in the base class. Since you're not doing that, you class remains abstract.