QtWebKit插件设计

/

软硬件环境

  • Ubuntu Kylin

概述

QtWebkit插件的设计可以分为两种情况。一种是MIME类型是application/x-qt-plugin或者application/x-qt-styled-widget,而另一种却无此限制,可以是任意类型。本篇主要介绍第一种,相比于第二种情形也更简单,只需要重新实现方法QObject* QWebPage::createPlugin(const QString &classid,const QUrl &url,const QStringList &paramNames,const QStringList &paramValues)。本文将详细介绍这二种方法的实现。

方法一

webPlugin.pro

  1. TEMPLATE = app
  2. QT += webkit
  3. HEADERS += myPlugin.h
  4. SOURCES += main.cpp myPlugin.cpp

main.cpp

  1. #include <QApplication>
  2. #include <QtGui>
  3. #include "myPlugin.h"
  4. int main(int argc, char *argv[])
  5. {
  1. QApplication app(argc, argv);
  1. MyWebView *webView = new MyWebView;
  1. webView->load(QUrl("Test.html"));
  2. webView->show();
  1. app.exec();
  2. }

myPlugin.h

  1. #ifndef MY_PLUGIN_H
  2. #define MY_PLUGIN_H
  3. #include <QWebPage>
  4. #include <QWebView>
  5. // Derive from QWebPage, because a WebPage handles
  6. // plugin creation
  7. class MyWebPage: public QWebPage
  8. {
  1. Q_OBJECT
  2. protected:
  1. QObject *createPlugin(
  2. const QString &classid,
  3. const QUrl &url,
  4. const QStringList &paramNames,
  5. const QStringList & paramValues);
  1. public:
  1. MyWebPage(QObject *parent = 0);
  2. };
  3. // Derive a new class from QWebView for convenience.
  4. // Otherwise you'd always have to create a QWebView
  5. // and a MyWebPage and assign the MyWebPage object
  6. // to the QWebView. This class does that for you
  7. // automatically.
  8. class MyWebView: public QWebView
  9. {
  1. Q_OBJECT
  2. private:
  1. MyWebPage m_page;
  1. public:
  1. MyWebView(QWidget *parent = 0);
  2. };
  3. #endif

myPlugin.cpp

  1. #include <QtCore>
  2. #include <QtGui>
  3. #include "myPlugin.h"
  4. MyWebPage::MyWebPage(QObject *parent):
  1. QWebPage(parent)
  1. {
  1. // Enable plugin support
  2. settings()->setAttribute(QWebSettings::PluginsEnabled, true);
  1. }
  1. QObject *MyWebPage::createPlugin(
  1. const QString &classid,
  2. const QUrl &,
  3. const QStringList &,
  4. const QStringList &)
  1. {
  1. QObject *result = 0;
  2. if(classid == "pushbutton")
  1. //qDebug()<<"pushbutton";
  2. result = new QPushButton();
  1. else if(classid == "lineedit")
  1. //qDebug()<<"lineedit";
  2. result = new QLineEdit();
  1. if(result)
  1. result->setObjectName(classid);
  1. return result;
  1. }
  1. MyWebView::MyWebView(QWidget *parent):
  1. QWebView(parent),
  2. m_page(this)
  1. {
  1. setPage(&m_page);
  1. }

Test.html

  1. <html>
  1. <head>
  1. <title>QtWebKit Plug-in Test</title>
  1. </head>
  2. <body>
  1. <object type="application/x-qt-plugin" classid="pushbutton" name="mybutton" height=300 width=500>
  2. </object>
  1. </body>
  1. </html>

编译运行

  1. qmake
  2. make
  3. ./webPlugin

-01.png)

方法二

这种方法相比第一种情形应用更加灵活,不限制MIME类型,扩展性也更好。

main.cpp

  1. #include <QtGui/QApplication>
  2. #include "pluginexample.h"
  3. int main(int argc, char *argv[])
  4. {
  5. QApplication app(argc, argv);
  6. PluginExample pE;
  7. pE.show();
  8. return app.exec();
  9. }

pluginexample.h

  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3. #include <QtGui/QMainWindow>
  4. #include <QtCore/QDir>
  5. #include <QtCore/QUrl>
  6. #include <QtGui/QWidget>
  7. #include <QtGui/QVBoxLayout>
  8. #include <QtGui/QFrame>
  9. #include <QtGui/QDesktopServices>
  10. #include <QtWebKit/QWebView>
  11. #include <QtWebKit/QWebPage>
  12. #include <QtWebKit/QWebFrame>
  13. #include <QtWebKit/QWebSettings>
  14. #include <QtCore/QDebug>
  15. #include "webpluginfactory.h"
  16. class QWebView;
  17. class PluginExample : public QMainWindow
  18. {
  19. Q_OBJECT
  20. public:
  21. PluginExample(QWidget *parent = 0);
  22. ~PluginExample();
  23. private:
  24. QWebView* m_webView;
  25. QWebView* createWebView();
  26. };
  1. #endif // MAINWINDOW_H

pluginexample.cpp

  1. #include "pluginexample.h"
  1. /**
  • Plug-in example main class.
    /
    PluginExample::PluginExample(QWidget
    parent)
  1. : QMainWindow(parent)
  2. {
  3. // Create the central widget and set it.
  4. QFrame* cW = new QFrame(this);
  5. setCentralWidget(cW);
  6. // Set the layout to the central widget.
  7. QVBoxLayout* layout = new QVBoxLayout(cW);
  8. cW->setLayout(layout);
  9. layout->setMargin(0);
  10. layout->setSpacing(0);
  11. // Create the webview which will be used to display the page.
  12. m_webView = createWebView();
  13. // Add it to the layout.
  14. layout->addWidget(m_webView);
  15. m_webView->show();
  16. }
  17. PluginExample::~PluginExample()
  18. {
  19. }
  20. /**
  • Creates a new webview
    /
    QWebView
    PluginExample::createWebView()
    {

    1. //使能QWebView的Javascript和Plugins属性
    2. QWebSettings* defaultSettings = QWebSettings::globalSettings();
    3. // We use JavaScript, so set it to be enabled.
    4. defaultSettings->setAttribute(QWebSettings::JavascriptEnabled, true);
    5. // Plugins must be set to be enabled to use plug-ins.
    6. defaultSettings->setAttribute(QWebSettings::PluginsEnabled,true);
    7. QWebView* webView = new QWebView(this);
    8. /*
    9. * We also pass the web plugin factory to the webview.
    10. */
    11. WebPluginFactory* factory = new WebPluginFactory(this);
    12. webView->page()->setPluginFactory(factory);
    13. webView->load(QUrl("index.html"));
    14. return webView;

    }

WebPluginFactory是插件工厂类,主要需要实现的就是QWebPluginFactory中的两个虚函数:
virtual QObject *create(const QString &mimeType, const QUrl &url,
const QStringList &argumentNames,const QStringList & argumentValues )const = 0;virtual QList plugins () const = 0;
create() 方法则根据mimeType等参数来决定创建相应的插件,而plugins()方法是获取所有可用的插件列表。

webpluginfactory.h

  1. #ifndef WEBPLUGINFACTORY_H
  2. #define WEBPLUGINFACTORY_H
  3. #include <QWebPluginFactory>
  4. #include <QLabel>
  5. #include <QDebug>
  6. #include <QUrl>
  7. #include <QWebView>
  8. #include <QWebFrame>
  9. class WebPluginFactory : public QWebPluginFactory
  10. {
  11. Q_OBJECT
  12. public:
  13. explicit WebPluginFactory(QObject *parent = 0);
  14. QObject * create(const QString & mimeType,
  15. const QUrl & url,
  16. const QStringList & argumentNames,
  17. const QStringList & argumentValues) const;
  18. QList<QWebPluginFactory::Plugin> plugins () const;
  19. signals:
  20. public slots:
  21. private:
  22. QList<QWebPluginFactory::Plugin> _plugins;
  23. };
  24. #endif // WEBPLUGINFACTORY_H

webpluginfactory.cpp

  1. #include <QtDebug>
  2. #include "webpluginfactory.h"
  3. WebPluginFactory::WebPluginFactory(QObject *parent) :
  4. QWebPluginFactory(parent)
  5. {
  6. // initialise the _plugins QList
  7. // so that we can use it later in the plugins() method
  8. QWebPluginFactory::Plugin plugin;
  9. plugin.name = "Qt webkit Plugin";
  10. QWebPluginFactory::MimeType mimetype;
  11. mimetype.description = "Embedded Qt Widget";
  12. mimetype.fileExtensions = QStringList();
  13. mimetype.name = "application/x-qt-exampleplugin";
  14. plugin.mimeTypes.append(mimetype);
  15. _plugins.append(plugin);
  16. }
  17. QObject * WebPluginFactory::create(const QString & mimeType,
  18. const QUrl & url,
  19. const QStringList & argumentNames,
  20. const QStringList & argumentValues) const
  21. {
  22. Q_UNUSED(url);
  23. if (("application/x-qt-exampleplugin" != mimeType)) {
  24. return new QObject();
  25. }
  26. /*
  27. *** 对plugin的相应操作 ****
  28. */
  29. Q_UNUSED(argumentNames);
  30. Q_UNUSED(argumentValues);
  31. qDebug()<<"This is an MIME=application/x-qt-exampleplugin.";
  32. qDebug()<<"All the parameters is:"<<argumentNames<<" and "<<argumentValues;
  33. return NULL;
  34. }
  35. /**
  • Returns supported plug-ins.
    • Currently, this function is only called when JavaScript
    • programs access the global plug-ins or MIME type objects.
      */
      QList WebPluginFactory::plugins () const
      {
      return _plugins;
      }

pluginexample.pro

  1. QT += core \
  2. gui \
  3. webkit
  4. TARGET = pluginexample
  5. TEMPLATE = app
  6. SOURCES += main.cpp\
  7. pluginexample.cpp \
  8. webpluginfactory.cpp
  9. HEADERS += pluginexample.h \
  10. webpluginfactory.h

测试页面index.html

  1. <html>
  2. <head>
  3. <title>Plugin example page</title>
  4. </head>
  5. <body id="body">
  6. <h2>QtWebKit plugin</h2>
  7. <object id="describePlugin"
  8. type="application/x-qt-exampleplugin"
  9. width="100%" height="80%">
  10. </object>
  11. </body>
  12. </html>

编译运行程序

  1. qmake
  2. make
  3. ./pluginexample

-01.png)

转载请注明作者和出处,并添加本页链接。
原文链接:http://xugaoxiang.com/post/4

给我留言