JavaScript screen object not available in QWebEnginePage

I have a Qt application that launches Webviews using QWebChannel .

In these views, I have JavaScript that does some things to fit / resize a window depending on the screen size. The screen object must provide such information. ( screen document )

But in my QWebEnginePage display object is empty during the whole loading process.

If I put a listener on a button to get screen.height somewhere on the page, now it works.

 //js local file called in html head //screen = {} document.addEventListener("load", function(event) { //screen = {} new QWebChannel(qt.webChannelTransport, function(channel) { //screen = {} //stuff channel.object.myClass.show(function(res){ //Qt function that calls the show() method of the QWebEngineView //screen = {} }); }); document.getElementById("myBtn").addEventListener("click", function(){ console.log("height:" + screen.height); // screen: 1440 }); }); 

So my question is: how can I access the screen values ​​at some point in my JavaScript?

+5
source share
1 answer

I think that at the moment you are trying to access your screen object, it is not available. In fact, your HTML file will be loaded directly, but other objects, such as JavaScript objects / programs or stylesheets (like CSS files) will be loaded asynchronously.

This is explained in the QWebEnginePage documentation:

html loads immediately; external objects are loaded asynchronously.

Thus, until your page is loaded, you cannot access this property. However, you can access it as soon as the page is loaded, for example, you really use the listener on the button. Another way to get these properties is to define the Q_INVOKABLE methods in your class to get them on the C ++ side ( see this answer ).

+1
source

Source: https://habr.com/ru/post/1247527/


All Articles