fixed plugin error for Qt5

This commit is contained in:
李 翔 2017-12-22 10:20:47 +08:00
parent 7bcede7d10
commit 55703a59ed
6 changed files with 79 additions and 12 deletions

View File

@ -35,7 +35,7 @@ public:
};
//Q_DECLARE_INTERFACE定义在在qobject.h中用来告诉Qt meta-object system 这个接口名称
Q_DECLARE_INTERFACE(PluginInterface, "com.ubuntukylin.Plugin.PluginInterface ")
Q_DECLARE_INTERFACE(PluginInterface, "com.ubuntukylin.Plugin.PluginInterface")
#endif // PLUGININTERFACE_H

1
shredmanager/shred.json Normal file
View File

@ -0,0 +1 @@
{}

View File

@ -28,9 +28,9 @@ class ShredManager : public QObject , PluginInterface
Q_OBJECT
Q_INTERFACES(PluginInterface)
//#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
// Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QImageIOHandlerFactoryInterface" FILE "raw.json")
//#endif
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
Q_PLUGIN_METADATA(IID "com.ubuntukylin.Plugin.PluginInterface" FILE "shred.json")//指定IID和.json文件
#endif
public:

View File

@ -120,19 +120,36 @@ int make_pid_file() {
int main(int argc, char *argv[])
{
// QApplication app(argc, argv);
QApplication app(argc, argv);
//单程序运行处理
QtSingleApplication app(argc, argv);
QSharedMemory mem("KA");
if (!mem.create(1)) {
qDebug() << QObject::tr("kylin-assistant had already running!");
return 1;
}
#ifdef QT_NO_DEBUG
qDebug() << "release mode";
#else
qDebug() << "debug mode";
#endif
//单程序运行处理
/*QtSingleApplication app(argc, argv);
if (app.isRunning())
{
qDebug() << QObject::tr("kylin-assistant had already running!");
return 0;
}
}*/
// QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
// QTextCodec::setCodecForCStrings(QTextCodec::codecForLocale());
// QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
// QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0))
QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
QTextCodec::setCodecForCStrings(QTextCodec::codecForLocale());
QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
#endif
if (make_pid_file()) {
exit(1);

View File

@ -20,6 +20,7 @@
#include "pluginmanager.h"
#include "../component/plugininterface.h"
#include <QDir>
#include <QApplication>
#include <QDebug>
PluginManager::PluginManager(void)
@ -39,9 +40,19 @@ PluginManager* PluginManager::Instance()
bool PluginManager::loadPlugin(QString plugin_path)
{
/*QDir pluginsDir(qApp->applicationDirPath());//QCoreApplication::applicationDirPath()
#ifdef QT_DEBUG
pluginsDir.cd("libs");
#else
pluginsDir.cd("../libs");
#endif*/
QDir pluginsDir(plugin_path + "/libs");
foreach (QString fileName, pluginsDir.entryList(QStringList("*.so"),QDir::Files)) {
QPluginLoader *pluginLoader = new QPluginLoader(pluginsDir.absoluteFilePath(fileName));
if (!QLibrary::isLibrary(fileName))
continue;
QPluginLoader *pluginLoader = new QPluginLoader(pluginsDir.absoluteFilePath(fileName));
QObject *plugin = pluginLoader->instance();
if (plugin) {//测试插件是否有效:使用 qobject_cast()测试插件是否给出了相应接口并进行类型转换,转换成接口对象指针.
PluginInterface* interface = qobject_cast<PluginInterface*>(plugin);
@ -49,6 +60,10 @@ bool PluginManager::loadPlugin(QString plugin_path)
QString guid = interface->getGuid();
plugin_map.insert(guid, pluginLoader);
}
else {
pluginLoader->unload();
pluginLoader->deleteLater();
}
}
else {
delete pluginLoader;

View File

@ -1,3 +1,6 @@
#ifndef PLUGINSMANAGER_H
#define PLUGINSMANAGER_H
#include <QMap>
#include <QPluginLoader>
@ -60,3 +63,34 @@ public:
private:
QMap<QString, QPluginLoader*> plugin_map;
};
/*#include "frameproxyinterface.h"
#include <QObject>
using namespace dcc;
class PluginsController : public QObject, public FrameProxyInterface
{
Q_OBJECT
public:
explicit PluginsController(QObject *parent = 0);
signals:
void pluginAdded(QWidget * const w);
void requestModulePage(const QString &module, const QString &page);
public slots:
void loadPlugins();
private:
void pushWidget(ModuleInterface * const, ContentWidget * const) {}
void setFrameAutoHide(ModuleInterface * const, const bool) {}
void setModuleVisible(ModuleInterface * const, const bool) {}
void showModulePage(const QString &module, const QString &page);
};*/
#endif // PLUGINSMANAGER_H