使用QWebEngine渲染图像。

时间:2022-09-10 19:06:48

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.

我希望在我的无头渲染器中替换QWebKit和QWebEngine。我使用load()初始化页面,并将一个槽连接到loadFinished()以生成最终的. png图像。这曾经适用于WebKit但失败的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".

“请求共享上下文的小部件没有窗口句柄”“QOpenGLWidget:不能在没有上下文共享的情况下使用”。

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

有人有使用QWebEngine渲染屏幕的例子吗?

2 个解决方案

#1


3  

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

我刚刚遇到了同样的问题,我通过在加载后显示QWebEngineView来解决它。

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

下面是帮助我的示例:http://doc.qt.io/qt-5/qwebengineview.html#详细信息。

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

I hope it will help you

我希望它能帮到你。

#2


2  

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..

@wlalele的回答帮助了你,你需要调用view->show(),但这不是唯一的问题。最后,我必须继承QWebEngineView并覆盖事件过滤器以监视更新请求。

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.

只有在接收到UpdateRequest事件之后,您才能保证访问视图()->呈现函数的页面。

#1


3  

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

我刚刚遇到了同样的问题,我通过在加载后显示QWebEngineView来解决它。

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

下面是帮助我的示例:http://doc.qt.io/qt-5/qwebengineview.html#详细信息。

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

I hope it will help you

我希望它能帮到你。

#2


2  

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..

@wlalele的回答帮助了你,你需要调用view->show(),但这不是唯一的问题。最后,我必须继承QWebEngineView并覆盖事件过滤器以监视更新请求。

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.

只有在接收到UpdateRequest事件之后,您才能保证访问视图()->呈现函数的页面。