PDA

View Full Version : Creating custom plugin



T4ng10r
30th August 2011, 19:51
Hi,
I want to create a plugin system. So - first I create Interface

class ShopInterface
{
public:
virtual ~ShopInterface() {}

virtual QStringList shopNames() const = 0;
virtual QIcon getShopIcon() const = 0;
virtual bool checkPrices(QString strCompName, double & dPrice, QString strURL );
};

Then in separate CMake/VS project plugin is created.

class Shop1Plugin : public QObject, public ShopInterface
{
Q_OBJECT
Q_INTERFACES(ShopInterface)
public:
virtual ~Shop1Plugin ();
virtual QStringList shopNames() const;
virtual QIcon getShopIcon() const;
virtual bool checkPrices(QString strCompName, double & dPrice, QString strURL );
};

File Shop1Plugin.h file is sent to MOC which reports 'Error: Undefined interface' in line with Q_INTERFACES().
Why? Using Qt version 4.7.3

Lykurg
30th August 2011, 20:02
Ehm, where is Q_DECLARE_INTERFACE?

T4ng10r
30th August 2011, 20:06
Oh, I forgot to add this. To be sure - this time its entire interface header.

#ifndef _CCS_SHOP_INTERFACE_INCLUDE_
#define _CCS_SHOP_INTERFACE_INCLUDE_
#include <QtCore/QStringList>
#include <QtGui/QIcon>

class ShopInterface
{
public:
virtual ~ShopInterface() {}

virtual QStringList shopNames() const = 0;
virtual QIcon getShopIcon() const = 0;
virtual bool checkPrices(QString strCompName, double & dPrice, QString strURL );
};
Q_DECLARE_INTERFACE(ShopInterface, "ShopInterface/1.0");

#endif //_CCS_SHOP_INTERFACE_INCLUDE_

And - of course - in implemented plugin there is correct #include.

wysota
30th August 2011, 20:15
Does the plugin have the Q_EXPORT_PLUGIN2 macro?

T4ng10r
30th August 2011, 21:20
Yes, but placed in cpp file. In plugin examples I see that all of them have this macro there.
On the other hand - it looks like MOC compiler tries to expand Q_INTERFACES define and have problem to find appropriate interface definitions.

wysota
30th August 2011, 21:22
Did you include the file containing the interface definition in the plugin header file?

T4ng10r
30th August 2011, 21:49
#ifndef _CCS_SHOP_1_INCLUDE_
#define _CCS_SHOP_1_INCLUDE_
#include <ShopInterface.h>
#include <QtCore/QtPlugin>

class Shop1Plugin : public QObject, public ShopInterface
{
Q_OBJECT
Q_INTERFACES(ShopInterface)
public:
virtual ~Shop1Plugin ();
virtual QStringList shopNames() const;
virtual QIcon getShopIcon() const;
virtual bool checkPrices(QString strCompName, double & dPrice, QString strURL );
};
#endif

It seems most likely that #include isn't processed. Like MOC can't finding Q_DECLARE_INTERFACE, but as you see - its there. I'll make test and put entire interface in one file with its realization.
PS. And it worked. If Interface is in the same file as its realization - it worked. Why MOC can't find this include? Maybe I should add this include to MOC processing line.

wysota
30th August 2011, 21:52
Make sure the directory containing ShopInterface.h is in INCLUDEPATH.

T4ng10r
30th August 2011, 22:02
I use CMake to create configurations and projects. And this is the first time that MOC requires includes. Now I now what need to be fixed.

hereiam
17th August 2012, 23:37
Make sure the directory containing ShopInterface.h is in INCLUDEPATH.

thanks really a great tip because in previous projects from me cmake always seached all header files automatically.

I had the same problem but it is fixed now.