QT调用Linux的读写函数,Qt和网页中的JavaScript函数相互调用的实现

下面的说明来自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{} tag is used to embed arbitrary content into a web page,

for example:

\code

\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{} 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调用方法的对象:

classIntelliPlugin:publicQWebPluginFactory

实现其对应的create()和plugins()方法:

QObject*IntelliPlugin::create(constQString&mimeType,constQUrl&,constQStringList&argumentNames,constQStringList&argumentValues)const

{qDebug()<m_mimeType){if(!mime.name.isEmpty()&&mime.name==mimeType){if(mimeType=="application/print-plugin"){qDebug()<

QList<:plugin>IntelliPlugin::plugins()const{Pluginplugin;plugin.name="application/print-plugin";plugin.description="JustforPrintMessagecalltest";plugin.mimeTypes.append(m_mimeType);QList<:plugin>plugList;plugList.append(plugin);returnplugList;}

在main函数中需要执行的代码:

view=newQWebView;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(newIntelliPlugin);view->load(QUrl("test_2.html"));view->show();

三、下面是test_2.html中JS调用Qt方法:

function print() {

var s5 = "请耐心等待,过号请到分诊台重新分诊,谢谢合作";

/*调用的Qt中的方法:classShinePrint::voidPrintMessag(QStringmsg)*/

var printResult = document.getElementById("Callobject").PrintMessag(s5);

if (printResult < 0)

alert("网络出现故障,打印失败,请联系管理员");

}

function JavaAlert() {

alert("这是JavaScript中的函数JavaAlert()被Qt程序调用才会出现的提示");

}

四、 在Qt中调用JS中的函数:

view->page()->mainFrame()->evaluateJavaScript("JavaAlert();");

源代码下载:

具体下载目录在 /2012年资料/4月/10日/Qt和网页中的JavaScript函数相互调用的实现/0b1331709591d260c1c78e86d0c51c18.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值