You Are Here:

Community: Wiki

This page was last modified on 3 September 2009, at 07:39.

CS001425 - Loading and initialising a Qt plug-in dynamically

From Forum Nokia Wiki


ID CS001425 Creation date June 16, 2009
Platform S60 3rd Edition, FP1, FP2
S60 5th Edition
Tested on devices Nokia 5800 XpressMusic
Category Qt for Symbian Subcategory Base/System


Keywords (APIs, classes, methods, functions): QPluginLoader, Q_DECLARE_INTERFACE, Q_INTERFACES, Q_EXPORT_PLUGIN2

Overview

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 QPluginLoader.

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 for S60 installed on your platform.


Important

  • When using Qt for S60 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 EPOCALLOWDLLDATA as true for the plug-ins (library) because Qt macros have initialised global data; see the plug-in project files for more information.


Preconditions

Project (.pro) file

The DLL (plug-in) needs to have to have at least the same set of capabilities as the process (application) that loads the plug-in.

Header (pluginwidget.h)

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)

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


Postconditions

The Qt plug-in is loaded and initialised dynamically.

Related Wiki Articles

No related wiki articles found

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fTalkE3aE4cargeE5fscreenE5fsaverX qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqfntypeZWikiContentQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZtypeQUqfntypeZWikiContentQ qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqfntypeZWikiContentQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ