I need to handle checked state of checkbox in QWebView (QWebPage).

More details:

There are some checkboxes in HTML. We want to make changes in application when some of them are checked. To do that, we need to know when did it change.

How to do it?

有帮助吗?

解决方案

It's not easy. Here is a workaround, it's imperfect but practical enough.

Create a subclass of QObject, e.g. Notifier. This will be your bridge from the web world to the native world. Whatever change you want to implement if a checkbox is checked should be placed here directly or you can forward it further down the chain via the signal/slot mechanism.

Inject an instance of this Notifier object into your web page using addToJavaScriptWindowObject. It will be attached to the global window object. Now, use DOM's addEventListener to add a new listener to the checkbox, the listener does nothing but to invoke the bridge's callback, e.g. window.notifier.doSomething(). This listener tweak can be carried out by executing a small JavaScript code using evaluateJavaScript.

This trick works because QtWebKit internal bridge understands a QObject instance (though introspection) and make its properties and slots accessible from the JavaScript world.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top