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
- TEMPLATE = app
-
QT += webkit
-
-
HEADERS += myPlugin.h
-
SOURCES += main.cpp myPlugin.cpp
main.cpp
- #include <QApplication>
-
#include <QtGui>
-
-
#include "myPlugin.h"
-
-
int main(int argc, char *argv[])
-
{
-
QApplication app(argc, argv);
-
-
MyWebView *webView = new MyWebView;
-
webView->load(QUrl("Test.html"));
-
webView->show();
-
-
app.exec();
-
}
myPlugin.h
- #ifndef MY_PLUGIN_H
-
#define MY_PLUGIN_H
-
#include <QWebPage>
-
#include <QWebView>
-
-
// Derive from QWebPage, because a WebPage handles
-
// plugin creation
-
class MyWebPage: public QWebPage
-
{
-
Q_OBJECT
-
protected:
-
QObject *createPlugin(
-
const QString &classid,
-
const QUrl &url,
-
const QStringList ¶mNames,
-
const QStringList & paramValues);
-
public:
-
MyWebPage(QObject *parent = 0);
-
};
-
-
// Derive a new class from QWebView for convenience.
-
// Otherwise you'd always have to create a QWebView
-
// and a MyWebPage and assign the MyWebPage object
-
// to the QWebView. This class does that for you
-
// automatically.
-
class MyWebView: public QWebView
-
{
-
Q_OBJECT
-
private:
-
MyWebPage m_page;
-
public:
-
MyWebView(QWidget *parent = 0);
-
};
-
-
#endif
myPlugin.cpp
- #include <QtCore>
-
#include <QtGui>
-
-
#include "myPlugin.h"
-
-
MyWebPage::MyWebPage(QObject *parent):
-
QWebPage(parent)
-
{
-
// Enable plugin support
-
settings()->setAttribute(QWebSettings::PluginsEnabled, true);
-
}
-
-
QObject *MyWebPage::createPlugin(
-
const QString &classid,
-
const QUrl &,
-
const QStringList &,
-
const QStringList &)
-
{
-
QObject *result = 0;
-
if(classid == "pushbutton")
-
//qDebug()<<"pushbutton";
-
result = new QPushButton();
-
else if(classid == "lineedit")
-
//qDebug()<<"lineedit";
-
result = new QLineEdit();
-
if(result)
-
result->setObjectName(classid);
-
return result;
-
}
-
-
MyWebView::MyWebView(QWidget *parent):
-
QWebView(parent),
-
m_page(this)
-
{
-
setPage(&m_page);
-
}
Test.html
- <html>
-
<head>
-
<title>QtWebKit Plug-in Test</title>
-
</head>
-
<body>
-
<object type="application/x-qt-plugin" classid="pushbutton" name="mybutton" height=300 width=500></object>
-
</body>
-
</html>
编译运行
qmake
make
./webPlugin
阅读(2942) | 评论(0) | 转发(0) |