QT plugin interface inherits QObject
-
@mgreenish
I keep poking my nose in where I know nothing. But posts like http://lists.qt-project.org/pipermail/interest/2014-May/012411.html seem to indicate not? They keep saying it must be "pure virtual".But I wouldn't listen to me, and you should await the experts....
-
@JonB said in QT plugin interface inherits QObject:
@mgreenish
I keep poking my nose in where I know nothing. But posts like http://lists.qt-project.org/pipermail/interest/2014-May/012411.html seem to indicate not? They keep saying it must be "pure virtual".But I wouldn't listen to me, and you should await the experts....
You don't need to be an expert to find information ;)
@JonB's interpretation is correct. The interface class cannot inherit QObject, but the implementation can.
See the example at http://doc.qt.io/qt-5/plugins-howto.html --
FilterInterface
cannot inherit QObject, butExtraFiltersPlugin
inherits bothFilterInterface
and QObject.@mgreenish said in QT plugin interface inherits QObject:
Can an interface class for a qt plugin (that extends the main application's functionality) inherit QObject?
What is your end-goal? Why would you like the interface class itself to inherit QObject?
-
@mgreenish
Hi and Welcome!I agree with @JonB... what is your end goal. By definition, interfaces define access points to provide functionality and should NOT and should NEVER derive from QObject. Ideally, your interface should be pure virtual and contain absolutely no code or data.
Once you have established an interface, your derived implementation can also be derived from QObject (and should) for the plugin framework to work.
Qt has very nice documentation about the plugin framework and a simple example. Please refer to it for information and cut-n-paste code!
-
Thank you for the feedback. I did search before adding my post but as JonB, I only found posts that seemed to 'indicate' not. Thanks to JKSH & Buckwheat for clarifying.
I wasn't sure how to use the signals & slots mechanism to trigger events in my main application from my plugin. But given your feedback, I figured it out:
QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName)); QObject *plugin = pluginLoader.instance(); if( plugin ) { reactionsserverInterface = qobject_cast<ReactionsServerInterface *>(plugin); if( reactionsserverInterface ) { reactionsserverInterface->setParentCW(this); connect( this, SIGNAL(effectsSent(QJsonDocument)), plugin, SLOT(saveReactionFile(QJsonDocument)) ); connect( plugin, SIGNAL(reactionFileNameUpdated(QString)), this, SLOT(updateReactionFileName(QString) ) ); return true; } }
I was trying to tie back to reactionsserverInterface and that didn't work, but tying back to plugin worked.
Thanks!