我正在尝试使用Qt4的WebKit端口/实现编写一个简单的日志查看器.我的
HTML代码如下所示:
http://pastie.org/613296
更具体地说,我试图找出如何调用在< script>中定义的add_message()函数.来自我的C代码的HTML文档中的部分.
// Doesn't work:
QWebElement targetElement = chatView->page()->mainFrame()->findFirstElement("head").firstChild("script");
// Function is not included,either...
qDebug() << targetElement.tagName() << targetElement.functions();
// The ultimate attempt in calling the function anyway:
QVariant functionResult = targetElement.callFunction("add_message");
解决方法
如果您使用的是Qt 4.5,请执行以下操作:
htmlView->page()->mainFrame()->evaluateJavaScript("add_message(); null");
注意:脚本末尾的null是出于性能问题. QWebFrame :: evaluateJavaScript返回QVariant,其中包含脚本中的最后一个值.评估脚本中的最后一个值可能非常耗时,因此在结尾处放置null会使其立即返回.