Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2096783
  • 博文数量: 229
  • 博客积分: 7217
  • 博客等级: 上校
  • 技术积分: 3224
  • 用 户 组: 普通用户
  • 注册时间: 2009-02-19 17:23
个人简介

个人主页https://xugaoxiang.com,微信公众号: Dev_Club 或者搜索 程序员Club

文章分类

全部博文(229)

文章存档

2017年(1)

2016年(20)

2015年(23)

2013年(1)

2012年(23)

2011年(68)

2010年(62)

2009年(31)

分类: LINUX

2011-10-24 11:53:10

Qt/WebKit::插件设计(上)已经介绍了QtWebKit插件设计中MIME类型为application/x-qt-plugin或者application/x-qt-styled-widget的情况,本文主要介绍另一种情形,即不限制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. };


  27. #endif // MAINWINDOW_H
pluginexample.cpp
  1. #include "pluginexample.h"

  2. /**
  3.  * Plug-in example main class.
  4.  */
  5. PluginExample::PluginExample(QWidget *parent)
  6.     : QMainWindow(parent)
  7. {
  8.     // Create the central widget and set it.
  9.     QFrame* cW = new QFrame(this);
  10.     setCentralWidget(cW);

  11.     // Set the layout to the central widget.
  12.     QVBoxLayout* layout = new QVBoxLayout(cW);
  13.     cW->setLayout(layout);
  14.     layout->setMargin(0);
  15.     layout->setSpacing(0);

  16.     // Create the webview which will be used to display the page.
  17.     m_webView = createWebView();

  18.     // Add it to the layout.
  19.     layout->addWidget(m_webView);

  20.     m_webView->show();
  21. }

  22. PluginExample::~PluginExample()
  23. {

  24. }

  25. /**
  26.  * Creates a new webview
  27.  */
  28. QWebView* PluginExample::createWebView()
  29. {
  30.     //使能QWebView的Javascript和Plugins属性
  31.     QWebSettings* defaultSettings = QWebSettings::globalSettings();
  32.     // We use JavaScript, so set it to be enabled.
  33.     defaultSettings->setAttribute(QWebSettings::JavascriptEnabled, true);
  34.     // Plugins must be set to be enabled to use plug-ins.
  35.     defaultSettings->setAttribute(QWebSettings::PluginsEnabled,true);

  36.     QWebView* webView = new QWebView(this);

  37.     /*
  38.      * We also pass the web plugin factory to the webview.
  39.      */
  40.     WebPluginFactory* factory = new WebPluginFactory(this);
  41.     webView->page()->setPluginFactory(factory);

  42.     webView->load(QUrl("index.html"));
  43.     return webView;
  44. }
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. /**
  36.  * Returns supported plug-ins.
  37.  * Currently, this function is only called when JavaScript
  38.  * programs access the global plug-ins or MIME type objects.
  39.  */
  40. QList<QWebPluginFactory::Plugin> WebPluginFactory::plugins () const
  41. {
  42.     return _plugins;
  43. }
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.     
  12.     </body>
  13. </html>
编译运行程序
qmake
make
./pluginexample





阅读(6110) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~