Differences in DPI [font size] in QWebView compared to all other QWidgets?

I have a QWebView that displays some HTML content, and I used CSS to style the text:

body { font-size: 10pt; } 

The QWebView window also has a QTextEdit field, for which I set the font as follows:

 QFont newFont; newfont.setPointSize(10); myEditField->setFont(newFont); 

Unfortunately, the text displayed in QWebView is slightly larger than the text displayed in QTextEdit . I have a feeling that this is due to the fact that DPI settings are different in QWebView .

XqIJP.png

Is there a way to get the same font sizes displayed for both QWebView and QTextEdit ?

Thanks!

+4
source share
2 answers

An explanation of this behavior was given at bugreports :

WebKit seems to be accepting 96 dpi as a fixed resolution. If this is the way web content is being developed, we have a problem with this because there are other people who expect WebKit to display web content, for example in web browsers. See also https://www.webkit.org/blog/57/css-units/

They proposed two solutions:

QWebView provides setZoomFactor and setTextMultiplier , which I believe can be used to get the desired behavior (QWidget matching).

You can calculate the scaling factor and text multiplier using the current DPI:

 QWidget* window = QApplication::desktop()->screen(); const int horizontalDpi = window->logicalDpiX(); m_view->setZoomFactor(horizontalDpi / 96.0); 

Using QWebSettings::ZoomTextOnly , you can apply scaling to text only.

+2
source

This is a bit complicated. I found a good way to get accurate DPI measurements from QApplication, for example:

 QWidget* window = QApplication::desktop()->screen(); int horizontalDpi = window->logicalDpiX(); 

Regarding the font, you can use QWidget::fontMetrics() to get a good font metric.

I think a combination of both will give you some consistency between your webview and text editing.

Good luck.

+1
source

All Articles