QWebView: notify python when I clicked on a certain web eleemnt

Gelonida gelonida at gmail.com
Wed Feb 23 20:32:17 EST 2011


Damjan,


Thanks once more for your help.
You pointed me in the right direction.

On 02/24/2011 12:25 AM, Ian Kelly wrote:
> On Wed, Feb 23, 2011 at 4:02 PM, Gelonida <gelonida at gmail.com> wrote:
>>> so make a Python object with some methods you'd wish to call, then
>>> evaluate some small JS that will bind the click event on your element to
>>> the method of the Python object.
>>>
>>
>> OK, but how do I make sure, that I don't override an existing binding to
>> the element's click() event?
> 
> If the element has an existing binding, then create a new JS function
> that calls both the Python method and the original event handler, and
> bind that function to the event.

Not sure if it can be done easier, but what I roughly do now is:

top_frame = page.mainFrame()
my_qt_object = MyQtObject()
top_frame.addToJavaScriptWindowObject("pyObj", my_qt_object)
field = top_frame.findFirstElement(path)

js_inject_after = """{
    var that = this;
    var prev_action = this.%(evt)s;
    if (prev_action == null){
        prev_action = function(){};
    }
    this.%(evt)s = function(){
        prev_action();
        %(action)s;
    };
}"""

prms = dict(
    evt="onclick",
    action=""" pyObj.action(that.id+ "clicked"); """
)

field.evaluateJavaScript(js_inject_after % prms)








More information about the Python-list mailing list