QtWebkit中浏览器插件的设计-3

上一篇讲到可以通过扩展QWebPage接口进行动态载入插件,但是插件的接口并没有明确,这一篇通过介绍自定义的接口来实现插件的动态载入。

首先是接口的定义:

class WebKitPluginInteface

{

    public:

        virtual ~WebKitPluginInteface(){};

        virtual QList<QWebPluginFactory::Plugin> plugins()const =0;

        virtual QObject *create(const QString &mimeType,

                const QUrl &url,

                const QStringList &argumentNames,

                const QStringList &argumentValues) const =0;

};

 

Q_DECLARE_INTERFACE(WebKitPluginInteface, "baizx.cnblogs.com/1.0")

 

这样自定义的插件就可以通过实现这个接口来实现定制的插件。下面是一个例子:

class TestPlugin :public QObject,public WebKitPluginInteface

{

Q_OBJECT

Q_INTERFACES(WebKitPluginInteface)

    public:

        TestPlugin(QObject * parent=0): WebkitPlugin(parent){};

        virtual ~TestPlugin(){};

        virtual QList<QWebPluginFactory::Plugin> plugins()const ;

        virtual QObject *create(const QString &mimeType,

                const QUrl &url,

                const QStringList &argumentNames,

                const QStringList &argumentValues) const ;

};

 

QList<QWebPluginFactory::Plugin> TestPlugin::plugins()const

{

    QList<QWebPluginFactory::Plugin> plugins ;

    QWebPluginFactory::Plugin plugin;

    QWebPluginFactory::MimeType mimeType;

    QStringList strings;

    plugin.name="testplugin";

    plugin.description="testplugin !!!";

    mimeType.name="application/x-textedit";

    mimeType.description="test textedit";

    strings.append(".etxt");

    mimeType.fileExtensions=strings;

    QList<QWebPluginFactory::MimeType> mimeTypes;

    mimeTypes.append(mimeType);

    plugin.mimeTypes=mimeTypes;

    plugins.append(plugin);

    return plugins;

}

QObject *TestPlugin::create(const QString &mimeType,

                const QUrl &url,

                const QStringList &argumentNames,

                const QStringList &argumentValues) const

{

    QTextEdit * edit= new QTextEdit();

edit->setObjectName("我是插件");

    edit->setPlainText(mimeType + url.toString() );

    Q_UNUSED(argumentNames);

    Q_UNUSED(argumentValues);

    return edit;

}

 

这样一个简单的插件就创建完毕了,具体实际应用中可能会用到很到参数,并且会载入实际的内容,这里只是一个演示

你可能感兴趣的:(webkit)