Search code examples
c++qtqtwebkit

QtWebkit synchronous loading


I'm using a QWebPage without a QWebView because I want to render the contents of an HTML file onto a QPixmap/QImage.

I want the loading of the page to be done synchronously, not asynchronously which is the default. The default way is to call QWebFrame::setHtml() or QWebFrame::setContent(), but this loads images asynchronously. What I want is some sort of blocking function call, something like QWebFrame::waitUntilLoadFinished() after which I could just call render() and be done with it.

I can't find a way to do this. Am I missing something?


Solution

  • If anyone's interested, I implemented this using a special "PageRasterizer" class.

    The class creates a QWebPage in the constructor and sets a bool loading flag to false. A connect() call connects the loadFinished signal to a member slot that merely sets the loading flag to true.

    A special RenderPage() member function that returns an image does all the work: it accepts the HTML string and calls setHtml(). After that comes a while loop that waits on the flag; while the flag is false, qApp->processEvents() is called so signals get emitted and the flag setting slot is eventually called. When it is, the loop breaks and now you can render the page to a QImage (don't forget to set the flag back to false before returning).

    If you're interested in the rendering process, look at this Qt example (the Thumbnailer::render() function).

    For bonus points, you can make this class a functor.