How can I make QWebView/QWebPage default to serif font?

4.6k views Asked by At

Using PyQt4, I create a QWebView and then load it with its setHtml() method. The HTML is displayed correctly in most respects but always using the default sans-serif font.

When the identical HTML plain text is loaded by an external browser e.g. Firefox, it displays in the default serif font. I'd like the webview to do the same.

I've read the doc for QWebView, QWebPage, and QWebSettings and don't see any way to set the default "standard" or "proportional" font, comparable to a browser's preference setting. I looked at QStyleSheets but they don't seem to apply to QWebView/WebPage.

2

There are 2 answers

2
ekhumoro On

To set the default font for all web pages, use QWebSettings to change the font family for the StandardFont, and to change the font size for the DefaultFontSize:

settings = QtWebKit.QWebSettings.globalSettings()
settings.setFontFamily(QtWebKit.QWebSettings.StandardFont, 'Times New Roman')
settings.setFontSize(QtWebKit.QWebSettings.DefaultFontSize, 12)
9
Dan Milburn On

QWebSettings appears to have everything you need:

enum FontFamily { StandardFont, FixedFont, SerifFont, SansSerifFont, CursiveFont, FantasyFont }
void setFontFamily ( FontFamily which, const QString & family )

I haven't tested it, but what are you missing? You might want to look at the Arora web browser based on Qt Webkit.