Zoom feature for QWebEngine does not work

醉酒当歌 提交于 2019-12-24 00:48:05

问题


I created simple project for displaying local .html page. I used Qt5.4 with QWebView there. But after switching to Qt5.6 I noticed that Qt WebKit is deprecated and not supported any more. Thus I decided to replace Qt WebKit functionality with one from the Qt WebEngine.

After replacing QWebView with QWebEngineView I investigated that setZoomFactor method has no effect. Is it known issue? How can I handle with this?

EDIT: An interesting thing have been investigated recently. I use setHtml method for setting content of local .html files to my QWebEngineView. These files also contain references to images. So I set baseUrl parameter as a relative path to required images. In that case using of setZoomFactor method has no effect.

But when I don't set relative path to images as parameter, images are absent on QWebEngineView but zoom functionality works. Any ideas what is wrong here?


回答1:


It seems to be a known bug in this version of Qt. You can check by yourself here : Qt Bug 51992.

Basically, it is said that :

This looks like a known glitch that is currently happening because of the Chromium API that we use for setting the zoom factor.

And also :

Chromium limits the zoom factor to a maximum of 5.0 - any calls with a number higher than that will have no effect.

Hope that will help you.




回答2:


Setting zoomFactor for QML WebEngineView in Qt 5.11 using QML zoomFactor property or C++ setZoomFactor (private-API) did not work as expected. I discovered from comments in QT Bug 51992 that it works when set after a page load.

QML solution:

    WebEngineView {
        // ...
        onLoadingChanged: {
            zoomFactor = 0.75
        }
    }

QWebEngineView solution: connect to the loadFinished signal, and set zoomFactor after each page load:

main.cpp (after engine.load call):

QWebEngineView *webView;  // = ...
QObject::connect(webView, &QWebEngineView::loadFinished,
                 [=](bool arg) {
    webView->setZoomFactor(zoomFactor);
});


来源:https://stackoverflow.com/questions/36518896/zoom-feature-for-qwebengine-does-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!