Loading and initialising a Qt plug-in dynamically
Overview[edit | edit source]
Qt applications can be extended with Qt plug-ins. This code snippet demonstrates how to load and initialise plug-ins dynamically in your application using 76ytuiytuityutyutututyutyutyu.
Defining the plug-in interface and implementing the plug-in are described in additional snippet articles.
Note: In order to use this code, you need to have Qt installed on your platform.
Important[edit | edit source]
- When using Qt libraries from the (qt_libs_armv5_udeb.sisx) on a device, be sure to build both the plug-in and the loading application in DEBUG(UDEB) mode. This is because Qt uses a build key for each plug-in to ensure that only compatible plug-ins are loaded, and the build mode [debug|release] is part of that build key.
- In Symbian we have to define the variable 76ytuiytuityutyutututyutyutyu as true for the plug-ins (library) because Qt macros have initialised global data; see the plug-in project files for more information.
Preconditions[edit | edit source]
Project (.pro) file[edit | edit source]
The DLL (plug-in) needs to have at least the same set of capabilities as the process (application) that loads the plug-in.
Header (pluginwidget.h)[edit | edit source]
Needed headers, methods and variables.
// Qt plugin loader that loads plugins dynamically
- include <QPluginLoader>
- include <QtGui>
// Our defined plugin interface ExamplePluginInterface
- include "exampleplugininterface.h"
// Methods
bool loadPlugins(QString pluginDir);
void createPlugins();
void unloadPlugins();
private:
// Arry to store all plugins
QList<QPluginLoader*> plugins;
Source (yourapp.cpp)[edit | edit source]
Load the plug-ins.
bool YourApp::loadPlugins(QString pluginDir)
{
QDir pluginsDir(QLibraryInfo::location
(QLibraryInfo::PluginsPath));
// "exampleplugins" is the folder where you are exported plugins
// by Qt macro Q_EXPORT_PLUGIN2(exampleplugins, YourPlugin);
pluginsDir.cd("exampleplugins");
foreach (QString fileName, pluginsDir.entryList(QDir::Files))
{
// Create plugin loader
QPluginLoader* pluginLoader =
new QPluginLoader(pluginsDir.absoluteFilePath(fileName));
// Store loader to array
plugins.append(pluginLoader);
// Load plugin
bool ret = pluginLoader->load();
if (!ret)
{
QMessageBox::information(this, "YourApp",
QString("Could not load plugin %1").arg(fileName));
}
}
// Did we found any plugins?
if (plugins.isEmpty())
return false;
else
return true;
}
Create the plug-ins.
void YourApp::createPlugins()
{
// Show data of all media plugins in different tabs
for (int i=0 ; i<plugins.count() ; i++)
{
QPluginLoader* pluginLoader = plugins[i];
// Create plugin instance
QObject *plugin = pluginLoader->instance();
if (plugin)
{
// Plugin instance created
// Cast plugin to ExamplePluginInterface,
// that is common for all plugins
ExamplePluginInterface* pluginIF
= qobject_cast<ExamplePluginInterface*>(plugin);
// Signal / slot, if needed
//QObject::connect(this, SIGNAL(send(QString *)),
// pluginIF, SLOT(someSlot(QString *)));
}
else
{
// Could not create plugin instance, delete pluginloader
delete pluginLoader;
plugins.removeAt(i);
i--;
}
}
}
Unload the plug-ins.
void YourApp::unloadPlugins()
{
// Unload plugins and clear plugin array
foreach (QPluginLoader* pluginLoader, plugins)
{
pluginLoader->unload();
delete pluginLoader;
}
plugins.clear();
}
See also[edit | edit source]
- For more information about plug-ins, see How to Create Qt Plugins.
Postconditions[edit | edit source]
The Qt plug-in is loaded and initialised dynamically.