Qt4: How to call JavaScript functions on a page with C ++ through QtWebkit?

I am trying to write a simple log viewer using the port / implementation of Qt4 WebKit. My HTML code is as follows:

http://pastie.org/613296

In particular, I'm trying to figure out how to call the add_message () function, which is defined in a section <script>in an 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");
+5
source share
1 answer

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

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

: null script . QWebFrame::evaluateJavaScript QVariant script. script , null .

+12

All Articles