在最近的一个项目中,我使用了以下技术:
void VideoWebPage::urlLoaded(bool ok)
{
static const QString javascript =
"function installCallbacks() " \
"{ " \
" var videoTags = document.getElementsByTagName('object'); " \
" for (var i = 0; i < videoTags.length; ++i) " \
" { " \
" if (videoTags[i].type == 'application/x-qt-plugin') " \
" { " \
" if (videoTags[i].playing) " \
" { " \
" videoTags[i].playing.connect(playingSlot); " \
" } " \
" } " \
" } " \
"} " \
\
"function playingSlot(videoId) " \
"{ " \
" var playEvent=document.createEvent('Events'); " \
" playEvent.initEvent('play', true, false); " \
" document.getElementById(videoId).dispatchEvent(playEvent); " \
"} " \
"installCallbacks(); ";
mainFrame()->evaluateJavaScript(javascript);
}
此方法查找所有<object>
标记并将playing
信号连接到 Javascript 函数playingSlot()
。该playingSlot()
函数依次创建一个Event
具有名称的对象play
并将其作为普通 DOM 事件进行调度。HTML 文件如下所示:
<html>
<head>
<script language="text/javascript">
void init()
{
document.getElementById('id1').addEventListener('play', onPlay);
}
void onPlay(event)
{
alert('playing');
}
</script>
</head>
<body onload='init()'>
<object id='id1' type='application/x-qt-plugin'>
</object>
</body>
</html>
这当然适用于 Qt 插件小部件。我没有用纯 HTML 测试它(即没有使用 Qt 插件)。