QWebEngineView - 加载完成后渲染

QWebEngineView - Render after loading finished

提问人:Vakkas Celik 提问时间:11/3/2023 更新时间:11/3/2023 访问量:39

问:

我在 QWebEngineView 上渲染图像时遇到了问题,其中 HTML 文档立即加载,导致闪烁,而外部对象异步加载。我参考了 QWebEngineView (https://doc.qt.io/qt-6/qwebengineview.html#setHtml) 的文档,发现 setHtml 函数似乎同时加载和渲染,使我很难在中间拦截进程。

我的理解是否正确,如果是这样,有什么潜在的解决方案可以防止闪烁并确保仅在页面加载完成后进行渲染?您的见解和建议将不胜感激。

我尝试了以下代码,但没有帮助。

class CustomWebEngineView : public QWebEngineView
{
    Q_OBJECT

public:
    CustomWebEngineView(QWidget* parent = nullptr) : QWebEngineView(parent)
    {
        page()->settings()->setAttribute(QWebEngineSettings::ShowScrollBars, false);
        page()->loadFinished.connect(this, &CustomWebEngineView::onPageLoadFinished);
    }

public slots:
    void loadHtmlWithImages(const QString &html)
    {
        setHtml(html);
    }

private slots:
    void onPageLoadFinished(bool success)
    {
        if (success)
        {
            // HTML content has finished loading along with its resources
            page()->toHtml([this](const QString &html)
            {
                // The HTML content is available after loading
                // You can perform any additional processing here if needed
                setHtml(html);  // Set the HTML content to the view after resources are loaded
            });
        }
    }
};

int main(int argc, char* argv[])
{
    QApplication app(argc, argv);

    CustomWebEngineView view;

    // Your HTML content with image sources
    QString htmlContent = R"(
        <html>
        <body>
        <img src="image1.jpg">
        <img src="image2.jpg">
        </body>
        </html>
    )";

    view.loadHtmlWithImages(htmlContent);
    view.show();

    return app.exec();
}
qt qtwebengine qwebengine 查看

评论


答: 暂无答案