下面的說明來自Qt源代碼中的qwebpluginfactory.cpp文件中的介紹說明,引用一下。
/*!
\class QWebPluginFactory
\since 4.4
\brief The QWebPluginFactory class is used to embed custom data types in web pages.
\inmodule QtWebKit
The HTML \c{<object>} tag is used to embed arbitrary content into a web page,
for example:
\code
<object type="application/x-pdf" data="http://qt.nokia.com/document.pdf" width="500" height="400"></object>
\endcode
QtWebkit will natively handle the most basic data types like \c{text/html} and
\c{image/jpeg}, but for any advanced or custom data types you will need to
provide a handler yourself.
QWebPluginFactory is a factory for creating plugins for QWebPage, where each
plugin provides support for one or more data types. A plugin factory can be
installed on a
QWebPage using QWebPage::setPluginFactory().
\note The plugin factory is only used if plugins are enabled through QWebSettings.
You provide a QWebPluginFactory by implementing the plugins() and the
create() methods.
For plugins() it is necessary to describe the plugins the
factory can create, including a description and the supported MIME types.
The MIME types each plugin can handle should match the ones specified in
in the HTML \c{<object>} tag of your content.
The create() method is called if the requested MIME type is supported. The
implementation has to return a new instance of the plugin requested for the
given MIME type and the specified URL.
The plugins themselves are subclasses of QObject, but currently only plugins
based on either QWidget or QGraphicsWidget are supported.
---------------------------------------------------------------------------------------------
一、 按照上面給出的說明,如果要實現一個可以被網頁中的JS調用的Qt方法,需要注意:
1. 需要繼承QWebPluginFactory來實現自己的plugin類
2. 重載自定義plugin類的create()和plugins()方法
3. create()要實現創建特定的工JS調用的對象
4. plugins() 需要返回適當的QList<>
5. JS調用的方法的對象需要繼承自QWidget或者QGraphicsWidget,而不是QObject
二、給出的實例代碼如下:
定義JS調用方法的對象:
class IntelliPlugin : public QWebPluginFactory
實現其對應的create()和plugins()方法:
QObject* IntelliPlugin::create(const QString &mimeType, const QUrl &, const QStringList &argumentNames, const QStringList &argumentValues) const
{
qDebug() << " IntelliPlugin::create()"<<endl;
foreach (MimeType mime, this->m_mimeType) {
if (!mime.name.isEmpty() && mime.name==mimeType) {
if (mimeType == "application/print-plugin") {
qDebug() << "create shine print"<<endl;
ShinePrint * pShinePrint = new ShinePrint(); /*ShinePrint就是JS調用方法的類*/
qDebug() << "finish create shine print."<<endl;
return pShinePrint;
}
}
}
return NULL;
}
QList<IntelliPlugin::Plugin> IntelliPlugin::plugins() const
{
Plugin plugin;
plugin.name = "application/print-plugin";
plugin.description = "Just for PrintMessage call test";
plugin.mimeTypes.append(m_mimeType);
QList<IntelliPlugin::Plugin> plugList;
plugList.append(plugin);
return plugList;
}
在main函數中需要執行的代碼:
view = new QWebView;
view->setWindowFlags(Qt::FramelessWindowHint);
view->setGeometry(50, 50, 1024, 768);
view->settings()->setAttribute(QWebSettings::PluginsEnabled, true);
view->settings()->setAttribute(QWebSettings::JavascriptEnabled, true);
view->page()->setPluginFactory(new IntelliPlugin);
view->load(QUrl("test_2.html"));
view->show();
三、下面是test_2.html中JS調用Qt方法:
<object type="application/print-plugin" data="http://www.sohu.com" width="1" height="1" id="Callobject">
</object>
<script type="text/javascript">
function print() {
var s5 = "請耐心等待,過號請到分診台重新分診,謝謝合作";
/*調用的Qt中的方法: class ShinePrint::void PrintMessag(QString msg)
*/
var printResult = document.getElementById("Callobject").PrintMessag(s5);
if (printResult < 0)
alert("網絡出現故障,打印失敗,請聯系管理員");
}
function JavaAlert() {
alert("這是JavaScript中的函數JavaAlert()被Qt程序調用才會出現的提示");
}
</script>
四、 在Qt中調用JS中的函數:
view->page()->mainFrame()->evaluateJavaScript("JavaAlert();");
源代碼下載:
免費下載地址在 http://linux.linuxidc.com/
用戶名與密碼都是www.linuxidc.com
具體下載目錄在 /2012年資料/4月/10日/Qt和網頁中的JavaScript函數相互調用的實現/