Hi
I am stuck while trying to implement an plugin-aware application. Mainly because I'm not sure it what I want to do is possible: I have a MDI area in my main window and I want to populate it with widgets obtained through a plugin interface. I've studied the plugin examples in the Qt documentation as well as the plugin interface described in "C++ GUI Programming with Qt" and they all differ from what I need because the plugins all return a object known to Qt, for example a QPixmap or a QPen or something like that. In my case, it needs to return a class that is defined by the creator of the plugin. I'll try to explain my problem in as much detail as possible, and there is probably an easy answer. Some code might make it clearer:
In my application, I have the following plugin interface:
class viewerInterface {
public:
virtual ~viewerInterface() { }
};
Q_DECLARE_INTERFACE(viewerInterface,"com.software_name.app_name.viewerInterface/0.1")
class viewerInterface {
public:
virtual ~viewerInterface() { }
virtual QStringList views() const = 0;
virtual QWidget * loadViewer(const QString &view_type, QString dbTable, QString dbFile) = 0;
};
Q_DECLARE_INTERFACE(viewerInterface,"com.software_name.app_name.viewerInterface/0.1")
To copy to clipboard, switch view to plain text mode
And an intended plugin for this interface is shown below:
class varMatrixReal
: public QWidget,
public Ui_varMatrixBase,
public viewerInterface
{ Q_OBJECT
Q_INTERFACES(viewerInterface)
public:
varMatrixReal
(QWidget *parent
= 0);
private slots:
void Close();
bool createDBConnection();
private:
// Variables
public:
// Details received from application side
};
#endif // !_VARMATRIXREAL_H_
class varMatrixReal : public QWidget, public Ui_varMatrixBase, public viewerInterface {
Q_OBJECT
Q_INTERFACES(viewerInterface)
public:
varMatrixReal(QWidget *parent = 0);
void setupVUI(QString, QString); // Setup viewer user interface
QStringList views() const;
QWidget * loadViewer(const QString &view_type, QString dbTable, QString dbFile);
private slots:
void Close();
bool createDBConnection();
private:
// Variables
QSqlTableModel *dataModel;
QSqlDatabase dbProject;
public:
// Details received from application side
QString dbTable;
QString dbFilename;
};
#endif // !_VARMATRIXREAL_H_
To copy to clipboard, switch view to plain text mode
where the classes it inherits from are the following:
QWidget - Needed since it will be a widget displayed in a MDI Area
Ui_varMatrixBase - GUI base class created with Qt designer
viewerInterface - The abstract interface base class
Now with this background the question is the following: When I want to use this plugin in my application, how is it possible to cast a pointer to the varMatrixReal class if the main application does not have any knowledge of the plugin except for the interface (or does it?).
The first step will be something like this:
if (viewerInterface*interface = qobject_cast<viewerInterface*>(loader.instance()))
interfaces.append(interface);
QPluginLoader loader(pluginDir.absoluteFilePath(fileName));
if (viewerInterface*interface = qobject_cast<viewerInterface*>(loader.instance()))
interfaces.append(interface);
To copy to clipboard, switch view to plain text mode
But after this, how is it possible to use the functions of the derived classes and not only the interface base class? I mean, to do this you should probably qobject_cast the interface pointer to a varMatrixReal class and add such an instance to the MDI Area, however how do I get the application to have knowledge of the varMatrixReal class? Or the more generic case where the plugin defined the needed derived class and the application does not have knowledge of this, in this case varMatrixReal.
I have the feeling it should be possible because the plugin is a library which contains the class definition in any case. Do I only need to link to it in the .pro file perhaps?
Thanks in advance, I hope the question is clear enough.
Jaco
Bookmarks