Interaction between pyqt5 QWebEngineView and js

new QWebChannel(qt.webChannelTransport,
function(channel) {
    {
        window.my_object = channel.objects.my_object;
        alert(my_object.test.myHello()); //  1
    }
});

after the above code uses python to insert into the page (html introduces qwebchannel.js can not be executed correctly, I don"t understand why), the code in 1 can output

correctly.
$("-sharpget-info").on("click",
function() {
    alert(my_object); //  2
});
The code in

2 outputs undefined,. Excuse me, why?

Jan.12,2022

does not have a $object?


html introduced qwebchannel.js cannot be executed correctly, do not understand the reason

what is wrong with this report? can you describe it in detail?

The code in
2 outputs undefined

it is likely that "Code 2" executes before "Code 1". You can view the objects on the current page through the browser's debugging tool.


The value returned by

pyqt must be received by a method. As shown in the above method, when
js calls the Python function, the parameter passed to python is the constructor by default. If you want to pass the custom parameter, you can pass it freely, but at the end of the parameter list, you must add a callback function. The custom parameter needs to be declared in the formal parameter list of the python function, and the callback function is the default and does not need to be declared. For the above errors, you can use the following two ways:

 pyjs.myHello(alert);

or

pyjs.myHello(function (res) {
                    alert(res)
                })
Menu