PDA

View Full Version : Plugin interface to allow plugin to call a member function of the application



schall_l
8th October 2010, 10:48
Hello,

while it is quite clear how to write an interface so that the application can interact with the plugin by calling some of it member functions, I would like to now how I can have the plugin interacting with the application by letting him call some member functions declared in the application.

The following code shows how the application could display some message in the plugin by having the application calling a member function of the plugin.


class MyInterface
{
public:
virtual ~MyInterface() {}
virtual QString displayInPlugin(const QString &message) = 0;
};

Q_DECLARE_INTERFACE(MyInterface, "myApplication/1.0");

Now I am looking for the other way around, give the plugin the possibility to call a member function of the application.

borisbn
8th October 2010, 12:38
You can do it in two ways ( at least ).
1. Declare a signal in a plugin, connect application's slot to it, and emit this signal
2. You can derive your application from another interface ( say: IMainProgram ), and give the pointer to this interface to your plugin


class IMainProgram {
public:
virtual void doSmth() = 0;
};
class Plugin
{
public:
Plugin( IMainProgram * mainProgram )
: m_mainProgram( mainProgram )
{}
void foo() {
m_mainProgram->doSmth();
}
protected:
IMainProgram * m_mainProgram;
};
class MainWindow : public QMainWindow, public IMainProgram {
...
virtual void doSmth() {}
...
void createPlugin() {
plugin = new Plugin( this );
}
};