3

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.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
user405
  • 579
  • 7
  • 13
  • Out of curiosity, why not set some CSS in the contents? Something as simple as `` should do the trick... – John Chadwick Jan 27 '12 at 02:31
  • 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 Jan 27 '12 at 04:12

2 Answers2

5

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)
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • The same thing does not work if html contains undefined font-family e.g. p{font-family:"xyz"}..In this case webpage is not using default font why ? – Ashish Oct 23 '12 at 11:27
  • @Ashish. Sorry - I don't know. Other people might do though, so you should ask a new question. – ekhumoro Oct 23 '12 at 15:31
0

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.

Dan Milburn
  • 5,600
  • 1
  • 25
  • 18
  • Nope. setFontFamily establishes the specific font-name to be used for the generic FontFamily, e.g. when I print the query fontFamily(QWebSettings.SerifFont) I get "Times New Roman". I can call setFontFamily(QWebSettings.SerifFont,"Palatino") and viola, fontFamily will echo back "Palatino." Big whoop -- the web page is still displayed using the default SansSerifFont. The question is, how to get QWebPage to render using SerifFont not SansSerifFont. – user405 Jan 27 '12 at 16:35
  • ...I should say, I think that's the issue, I could be wrong (I often am) – user405 Jan 27 '12 at 16:48
  • I may be missing something obvious here, but can you not just set the font face for SansSerifFont as well? – Dan Milburn Jan 27 '12 at 17:58
  • oooh -- set the sansSerifFont to "Palatino" -- my god is that allowed? my computer might explode -- wait I'll try it... – user405 Jan 27 '12 at 18:38
  • Nope. I did settings.setFontFamily(QWebSettings.SansSerifFont,QString("Palatino")) and read it back with settings.fontFamily, and it didn't make a difference. I also did QWebSettings.globalSettings().setFontFamily(QWebSettings.SansSerifFont,QString("Palatino")). No change in the displayed page, still in sans-serif. – user405 Jan 27 '12 at 18:47
  • So just in case there is some problem with "Palatino" as a family, I did self.settings.setFontFamily(QWebSettings.SansSerifFont,self.settings.fontFamily(QWebSettings.SerifFont)), that is, take the default family from Serif (which is Times New Roman) and install it on SansSerif. No change. – user405 Jan 27 '12 at 18:52
  • Following another line: QWebPage undoubtedly uses a QPainter to render and QPainter has a font. However the QPainter is not accessible from a QWebPage or QWebView. You can get/set the QPainter's renderHints but they only spec how the font is rendered, e.g. anti-aliased. Not the font itself. – user405 Jan 28 '12 at 00:49
  • Thanks for the tip & link to Arora. It's a big program, but I found the settings dialog where they show a QFontDialog and save the family name and size into a label, standardLabel. The trail ends there, I couldn't find where they did anything with that label. – user405 Jan 28 '12 at 01:26
  • @user405. Did you see my answer? It completely solves your problem. – ekhumoro Jan 28 '12 at 03:20