9

I'm looking to replace QWebKit with QWebEngine in my headless renderer. I initialise the page with load() and connect a slot to loadFinished() to generate the final .PNG image. This used to work fine with WebKit but fails with QWebEngine.

Code is as follows...

_webView = new QWebEngineView();

....

// Render the HTML to an image
QPainter painter(&image);
_webView->page()->view()->render(&painter);
painter.end();

I receive the following errors :

"Asking for share context for widget that does not have a window handle" "QOpenGLWidget: Cannot be used without a context shared with the toplevel".

Does anyone have an example of rendering a screen using QWebEngine?

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Richard Adams
  • 591
  • 1
  • 9
  • 23

2 Answers2

5

I just had the same problem, I solved it by showing the QWebEngineView after the load.

Here is the example that helped me : http://doc.qt.io/qt-5/qwebengineview.html#details

QWebEngineView *view = new QWebEngineView(parent);
view->load(QUrl("http://qt-project.org/"));
view->show();

I hope it will help you

wlalele
  • 333
  • 5
  • 16
5

So the answer from @wlalele helped, you do need to call view->show(), but that wasn't the only issue. In the end I had to inherit from QWebEngineView and override the event filter to monitor for update requests..

bool CustomWebEngine::eventFilter(QObject* object, QEvent* event)
{
    if (event->type() == QEvent::UpdateRequest)
    {
        emit updateFinished();
    }
}

Only after an UpdateRequest event has been received are you guaranteed to have access to the page in the view()->render function.

Richard Adams
  • 591
  • 1
  • 9
  • 23
  • 2
    Hi Richard, Please can you give more information on how you solved this? We are banging our heads against a wall! –  Jan 06 '16 at 10:24