Qt World Summit: Register Today!

QWebEngineView loadfinished is too quick

  • Hi. I'm converting PDF files to images (thumbpics actually) using pdf.js on QWebEngineView, and then rendering them on a QImage and saving it to a file.

    The thing is that of course I have to wait for the view to finish loading before rendering it to the QImage, and I'm having troubles with the signals.

    QWebEngineView::loadFinished triggers before the javascript is done, so I get a blank image.
    My working solution was using QWebEngineView::loadProgress and counting how many times it reads 100.
    It seems that loadProgress returns 100 4 times.
    1st when QWebEngineView has read the javascript, but not executed it (this is when loadFinished triggers)
    2nd when pdf.js has loaded its frame, but not the pdf
    3rd when pdf.js has loaded the pdf
    4th I've no idea what changed here. I see no difference.
    (these events are just suppositions based on what the rendered file shows, not based on documentation or anything more serious)

    Since it seems that by the 3rd 100 pdf.js has loaded the pdf, I should render it at that moment. However I'm not 100% sure that it will always be done by the time it reaches the 3rd 100, or that there will always be a 3rd 100. So it doesn't seem like a good idea to rely on this.

    I just want to make my code more robust. Is there a more reliable way to know when QWebEngineView has actually finished loading all the javascript?

  • @coyoteazul
    I don't know anything about what pdf.js does, and what its "frame" is. But you need a signal from the JavaScript once it is "ready", whatever that involves Can you leverage the https://doc.qt.io/qt-5/qwebenginepage.html#runJavaScript-2 overload, so that resultCallback in your host code gets called when pdf.js has done its job and is fully "ready" for you to proceed?

  • I couldn't manage to implement it with runjavascript because pdf.js is way more complex than I can manage at the moment, but your link made me realize that I was ignoring a big part of qwebengine. I think I can use runjavascript to inspect DOM and see if it has already loaded.
    Thank you!

Log in to reply