PDA

View Full Version : Plugin-signals-slots problem



Misenko
21st September 2008, 23:39
Hi all

I have application which load all plugins in directory plugins and show thier menus. I do this in class plugins:



bool Plugins::loadPlugins()
{
QDir dir("./plugins");
if (!dir.exists())
return false;

foreach (QString filename, dir.entryList(QDir::Files))
{
loader->setFileName(dir.absoluteFilePath(filename));
QObject *couldBeFilter = loader->instance();
if (couldBeFilter)
{
PluginInterface *plugin = qobject_cast<PluginInterface*>(couldBeFilter);
if (plugin)
{
plugins.insert(plugin->name(), plugin);
connect(plugin->menu(), SIGNAL(writeData(QByteArray)), this, SIGNAL(dataOut(QByteArray)));
}
}
}

return true;
}


Then I have class PluginInterface:



class PluginInterface
{

public:
virtual QString name() const = 0;
virtual Menu* menu() const = 0;
};

Q_DECLARE_INTERFACE(PluginInterface, "sk.kimle.PluginInterface/0.1")


And now I am writing plugin. First I inherit from this interface this class:



class ProgramViewer : public QObject, PluginInterface
{
Q_OBJECT
Q_INTERFACES(PluginInterface)

public:
ProgramViewer();
virtual ~ProgramViewer();
virtual QString name() const;
virtual Menu* menu() const;
private:
QString pluginName;
};


Function menu() return Menu object. It inherits from QWidget and have only virtual functions and slots:



class Menu : public QWidget
{
Q_OBJECT

public:
Menu(){}
~Menu(){}
virtual void showWindow() const = 0;
virtual void makeConnections() = 0;
public slots:
virtual void readData(QByteArray array) =0;
signals:
virtual void writeData(QByteArray array);
};


I have also class which inherits from Menu and this class has other widgets and functions.



class ProgramViewerMenu : public Menu
{
Q_OBJECT

public:
enum DataToSend
{
NewImage = 1,
Pixels,
Stop
};
ProgramViewerMenu();
~ProgramViewerMenu();
void showWindow() const;
void makeConnections();
bool startBroadcasting();
bool stopBroadcasting();
QByteArray* takeShot(WId id);
QByteArray* changePixels(QImage oldOne, QImage newOne);
public slots:
void readData(QByteArray array);
void broadcasting();
void capture();
void refreshList();
signals:
void writeData(QByteArray array);
private:
QLabel *title;
QPushButton *broadcast;
QPushButton *refresh;
QGridLayout *grid;
QListWidget *list;
QTimer *timerRefresh;
QTimer *timerBroadcast;
QImage screen;
WId id;
bool isBroadcasting;
};


In some functions of this class I want to emit writeData(QByteArray) signal which is connected in the first class. But I ve got this error:
In function `ZN17ProgramViewerMenu11qt_metacallEN11QMetaObject 4CallEiPPv':
In function `ZN17ProgramViewerMenu11qt_metacastEPKc':
undefined reference to `Menu::qt_metacall(QMetaObject::Call, int, void**)'
undefined reference to `Menu::qt_metacast(char const*)'

I do not know whats wrong. I am not sure about those virtual functions, signals and slots and also I do not know if it is possible to connect something to plugin. Can anybody solve this? Thanks.

Sorry about my english.

jpn
22nd September 2008, 08:00
Try re-running qmake.

Misenko
22nd September 2008, 19:20
I ve done this many times becouse I ve been trying to change virtual functions, signals, slots etc.

jpn
22nd September 2008, 20:44
Is menu.h listed in the .pro file?

Misenko
22nd September 2008, 20:52
No it wasnt but I added it but nothing chenged.

jpn
22nd September 2008, 20:58
Now re-run qmake once again. :)

Misenko
22nd September 2008, 22:38
Thanks it works, but I dont know why :D

But I have another problem. I cant load plugin in Plugins class with function loadPlugins().
It found file and get filepath but it cant create QObject instance and I dont know why.

jpn
23rd September 2008, 07:50
Thanks it works, but I dont know why :D
Headers containing the Q_OBJECT macro must be listed in the .pro file because qmake must create rules for running MOC on them.


But I have another problem. I cant load plugin in Plugins class with function loadPlugins().
It found file and get filepath but it cant create QObject instance and I dont know why.
Have you checked QPluginLoader::errorString()?

Misenko
23rd September 2008, 21:53
Thank you man. I didnt know about this errorString function :o
Ok problem solved. Plugin appears correctly.