最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Qt4: How to call JavaScript functions in a page from C++ via QtWebkit? - Stack Overflow

programmeradmin3浏览0评论

I'm trying to write a simple log viewer using Qt4's WebKit port/implementation. My HTML code looks like this:

More specifically, I'm trying to find out how to call the add_message() function which is defined in the <script> section in the HTML document from my C++ code.


// 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");

I'm trying to write a simple log viewer using Qt4's WebKit port/implementation. My HTML code looks like this:

http://pastie/613296

More specifically, I'm trying to find out how to call the add_message() function which is defined in the <script> section in the HTML document from my C++ code.


// 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");
Share Improve this question edited Aug 9, 2012 at 3:49 Blender 298k55 gold badges458 silver badges510 bronze badges asked Sep 11, 2009 at 7:42 BastiBenseBastiBense
Add a ment  | 

1 Answer 1

Reset to default 14

If you are using Qt 4.5 do it something like this:

htmlView->page()->mainFrame()->evaluateJavaScript("add_message(); null");

Note: null at the end of script is for performance issue. QWebFrame::evaluateJavaScript returns QVariant with last value in the script. Evaluating last value in the script may be really time consuming, so putting null at the end makes it return immediately.

发布评论

评论列表(0)

  1. 暂无评论