3
votes

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
Out of curiosity, why not set some CSS in the contents? Something as simple as <style>body{font:serif}</style> should do the trick... - John Chadwick
For the HTML documents to be edited by this app, there is a standard that they don't specify font or font-family, but default the proportional font. That's so the user can always control the look through browser prefs. What I can't figure out is, how to do a "browser prefs" equivalent with QWebPage. - user405

2 Answers

5
votes

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)
0
votes

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.