nmake clean + qmake again :]
this works for me:
interface.h:
#ifndef INTERFACE_H
#define INTERFACE_H
#include <QtPlugin>
class PluginInterface
: public QObject{
Q_OBJECT
public:
virtual void someMethod() = 0;
signals:
void someSignal();
};
Q_DECLARE_INTERFACE(PluginInterface, "pl.faldzip.PluginInterface")
#endif // INTERFACE_H
#ifndef INTERFACE_H
#define INTERFACE_H
#include <QtPlugin>
class PluginInterface : public QObject
{
Q_OBJECT
public:
PluginInterface(QObject *parent) : QObject(parent) {}
virtual void someMethod() = 0;
signals:
void someSignal();
};
Q_DECLARE_INTERFACE(PluginInterface, "pl.faldzip.PluginInterface")
#endif // INTERFACE_H
To copy to clipboard, switch view to plain text mode
myplugin.h:
#ifndef MYPLUGIN_H
#define MYPLUGIN_H
#include "plugin1_global.h"
#include "interface.h"
class MyPlugin : public PluginInterface
{
Q_OBJECT
Q_INTERFACES(PluginInterface)
public:
void someMethod();
};
#endif // MYPLUGIN_H
#ifndef MYPLUGIN_H
#define MYPLUGIN_H
#include "plugin1_global.h"
#include "interface.h"
class MyPlugin : public PluginInterface
{
Q_OBJECT
Q_INTERFACES(PluginInterface)
public:
MyPlugin(QObject *parent = 0);
void someMethod();
};
#endif // MYPLUGIN_H
To copy to clipboard, switch view to plain text mode
myplugin.cpp:
#include "myplugin.h"
MyPlugin
::MyPlugin(QObject *parent
) : PluginInterface(parent)
{
}
void MyPlugin::someMethod()
{
emit someSignal();
}
Q_EXPORT_PLUGIN2(myplugin, MyPlugin);
#include "myplugin.h"
MyPlugin::MyPlugin(QObject *parent)
: PluginInterface(parent)
{
}
void MyPlugin::someMethod()
{
emit someSignal();
}
Q_EXPORT_PLUGIN2(myplugin, MyPlugin);
To copy to clipboard, switch view to plain text mode
and this was one project (with pro file containing TEMPLATE = lib and CONFIG += plugin),
and second project - app project:
main.cpp:
#include <QtGui>
#include <QtCore>
#include <QtPlugin>
#include "interface.h"
{
Q_OBJECT
public:
SomeClass
(PluginInterface
*pi,
QObject *parent
= 0) : QObject(parent
), m_pi
(pi
) { connect(m_pi,
SIGNAL(someSignal
()),
SLOT(someSlot2
()));
}public slots:
void someSlot() { m_pi->someMethod(); }
void someSlot2() { qDebug("someSlot2"); }
private:
PluginInterface *m_pi;
};
int main(int argc, char *argv[])
{
PluginInterface *pi = qobject_cast<PluginInterface *>(obj);
if (!pi)
{
qDebug("plugin error");
return 1;
}
SomeClass sc(pi, &pb);
pb.connect(&pb, SIGNAL(clicked()), &sc, SLOT(someSlot()));
pb.show();
return a.exec();
}
#include "main.moc"
#include <QtGui>
#include <QtCore>
#include <QtPlugin>
#include "interface.h"
class SomeClass : public QObject
{
Q_OBJECT
public:
SomeClass(PluginInterface *pi, QObject *parent = 0) : QObject(parent), m_pi(pi) { connect(m_pi, SIGNAL(someSignal()), SLOT(someSlot2())); }
public slots:
void someSlot() { m_pi->someMethod(); }
void someSlot2() { qDebug("someSlot2"); }
private:
PluginInterface *m_pi;
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QPluginLoader loader(a.applicationDirPath() + QDir::separator() + "plugin1.dll");
QObject *obj = loader.instance();
PluginInterface *pi = qobject_cast<PluginInterface *>(obj);
if (!pi)
{
qDebug("plugin error");
return 1;
}
QPushButton pb("Button");
SomeClass sc(pi, &pb);
pb.connect(&pb, SIGNAL(clicked()), &sc, SLOT(someSlot()));
pb.show();
return a.exec();
}
#include "main.moc"
To copy to clipboard, switch view to plain text mode
so it shows a button, and when I click on it, it's callingSomeClass::someSlot() which is calling PluginInterface::someMethod(), whis is emitting someSignal() connected to someSlot2() so the "someSlot2" appears on the screen.
Bookmarks