×
Namespaces

Variants
Actions

动态装入并初始化Qt插件

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata

兼容于
平台:
Symbian

文章
dougcn 在 23 May 2009 创建
最后由 hamishwillee 在 11 Oct 2012 编辑

Needs-update.pngThis article needs to be updated: If you found this article useful, please fix the problems below then delete the {{ArticleNeedsUpdate}} template from the article to remove this warning.

Reasons: hamishwillee (15 Aug 2011)
Article referred to old Qt 4.5 releases - including "Garden". I've deleted obvious incorrect links, but still needs to be reviewed by native speaker who is also Qt expert. For example, we no longer use emulator or SDKs that work on top of Symbian SDKs, instead we use the Qt SDK

  • 平台:S60 3rd Edition, FP1, FP2
    S60 5th Edition
  • 设备:Nokia 5800 XpressMusic
  • 类别:Qt for Symbian
  • 子类:Base/System
  • 关键字:QPluginLoader, Q_DECLARE_INTERFACE, Q_INTERFACES, Q_EXPORT_PLUGIN2

Contents

概述

此代码片段演示如何装载及在应用程序中动态初始化插件(plugins)。 Qt应用可通过插件来扩展。这需要应用程序使用QPluginLoader检测和装载插件。 本Qt插件代码片段分为三个部分:定义插件接口、实现接口和使用刚实现的接口,在“参看”部分查看更多信息。

注意: 要使用这段代码,在你的平台上要装有Qt for S60。


重要提示

  • 在设备上使用Qt for S60 Garden 4.5发行版时,要核查编译模式:

是DEBUG/UDEB还是RELEASE/UREL编译。应用程序和插件必须是一致的编译目标。

  • qt_libs_armv5_udeb.sisx是ARMv5 UDEB编译,即它是DEBUG编译。当将这个例子编译成GCCE UDEB时可以工作并能初始化插件。
  • 在Symbian下,必须为插件(库)定义EPOCALLOWDLLDATA变量为true,因为Qt宏已初始化全局数据;具体查看插件项目文件。


项目.pro文件

没有特别的,不需要Symbian能力。


头文件(pluginwidget.h)

需要的头文件、方法和变量。

// 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;


源代码 (yourapp.cpp)

装载插件:

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;
}

创建插件:

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--;
}
}
}

卸载插件:

void YourApp::unloadPlugins()
{
// Unload plugins and clear plugin array
foreach (QPluginLoader* pluginLoader, plugins)
{
pluginLoader->unload();
delete pluginLoader;
}
plugins.clear();
}


参看


后置条件

Qt插件被动态装入并初始化了。

This page was last modified on 11 October 2012, at 04:19.
130 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×