I am developing under Windows and I have a Qt plugin project that produces a dll, and I have another app that can load that dll dynamically as a plugin via the usual Qt methods. Basically what I wanted to do was have the plugin report its status back to this app that's using it (i.e. the plugin emits a signal, the app receives it via a slot). So after I successfully detect and read in the plugin (i.e. the dll file), I do the usual connect call to set up the signal/slot (which returns true), but the connection doesn't seem to actually be working, as my slot code never gets executed in the app. I tried to strip down the pertinent code below.
UpgradeInterface.h (this is the interface a plugin extends):
class UpgradeInterface
: public QObject{
public:
virtual ~UpgradeInterface() {}
virtual bool performUpgrade() = 0;
signals:
void statusUpdate
(QString statusMsg
);
};
Q_DECLARE_INTERFACE(UpgradeInterface, "com.UpgradeInterface/1.0")
class UpgradeInterface : public QObject
{
public:
virtual ~UpgradeInterface() {}
virtual bool performUpgrade() = 0;
signals:
void statusUpdate(QString statusMsg);
};
Q_DECLARE_INTERFACE(UpgradeInterface, "com.UpgradeInterface/1.0")
To copy to clipboard, switch view to plain text mode
//----------------------------------------------------------
UpgradePlugin.h:
class UpgradePlugin
: public QObject,
public UpgradeInterface
{
Q_OBJECT
Q_INTERFACES(UpgradeInterface)
public:
bool performUpgrade();
signals:
void statusUpdate
(QString statusMsg
);
};
class UpgradePlugin : public QObject, public UpgradeInterface
{
Q_OBJECT
Q_INTERFACES(UpgradeInterface)
public:
bool performUpgrade();
signals:
void statusUpdate(QString statusMsg);
};
To copy to clipboard, switch view to plain text mode
//----------------------------------------------------
UpgradePlugin.cpp:
bool UpgradePlugin::performUpgrade()
{
emit statusUpdate("Upgrade started!");
return true;
}
Q_EXPORT_PLUGIN2(UpgradePlugin, UpgradePlugin)
bool UpgradePlugin::performUpgrade()
{
emit statusUpdate("Upgrade started!");
return true;
}
Q_EXPORT_PLUGIN2(UpgradePlugin, UpgradePlugin)
To copy to clipboard, switch view to plain text mode
//-------------------------------------------------
The following is part of the app code that detects/loads the plugin.
MyDialog.h:
{
Q_OBJECT
//...
//...
protected slots:
void statusUpdateFromUpgradePlugin
(QString statusMsg
);
};
class MyDialog : public QDialog
{
Q_OBJECT
//...
//...
protected slots:
void statusUpdateFromUpgradePlugin(QString statusMsg);
};
To copy to clipboard, switch view to plain text mode
//------------------------------------------------------
MyDialog.cpp:
void MyDialog
::loadPlugins(QDir dir
) {
// Load dynamic plugins
{
QObject *plugin
= loader.
instance();
if (plugin)
{
UpgradeInterface * iUpgrade = qobject_cast<UpgradeInterface *>(plugin);
if (iUpgrade)
{
bool success
= connect(iUpgrade,
SIGNAL(statusUpdate
(QString)),
this,
SLOT(statusUpdateFromUpgradePlugin
(QString)));
// returns true iUpgrade->performUpgrade();
}
}
}
}
void MyDialog
::statusUpdateFromUpgradePlugin(QString statusMsg
) {
cout << statusMsg; // Never gets here!
}
void MyDialog::loadPlugins(QDir dir)
{
// Load dynamic plugins
foreach (QString fileName, dir.entryList(QDir::Files))
{
QPluginLoader loader(dir.absoluteFilePath(fileName));
QObject *plugin = loader.instance();
if (plugin)
{
UpgradeInterface * iUpgrade = qobject_cast<UpgradeInterface *>(plugin);
if (iUpgrade)
{
bool success = connect(iUpgrade, SIGNAL(statusUpdate(QString)), this, SLOT(statusUpdateFromUpgradePlugin(QString))); // returns true
iUpgrade->performUpgrade();
}
}
}
}
void MyDialog::statusUpdateFromUpgradePlugin(QString statusMsg)
{
cout << statusMsg; // Never gets here!
}
To copy to clipboard, switch view to plain text mode
Bookmarks