2016-11-23 36 views
1

我繼承了QWebEngineView類:QWebEngineView:在Mac OSX anormal行爲

class WebViewPerso(QtWebEngineWidgets.QWebEngineView): 


    def __init__(self, parent=None): 

     super(WebViewPerso, self).__init__(parent) 
     print(self.settings().fontFamily(QtWebEngineWidgets.QWebEngineSettings.StandardFont)) 

     # Get the default font and use it in the view 
     self.settings().setFontFamily(
      QtWebEngineWidgets.QWebEngineSettings.StandardFont, 
      self.font().family()) 
     print(self.settings().fontFamily(QtWebEngineWidgets.QWebEngineSettings.StandardFont)) 

在Linux上,兩個打印語句返回:

DejaVu Serif 
Sans Serif 

它清楚地表明,我改變了字體爲視圖,並且視圖現在使用正確的字體。

隨着OSX完全一樣的線,我得到:

Times 
.SF NS Text 

這也預示字體被改變。但是,視圖仍然使用Times字體

我也試過在設置欄:

FixedFont 
SerifFont 
SansSerifFont 
CursiveFont 
FantasyFont 
PictographFont 

但我WebEngineView仍然使用Times字體。

你有什麼想法爲什麼我觀察到這種行爲?

回答

0

嘗試一些由它繼承的方法,而不僅僅是從QWebEngineView。

  1. 東西從here。例如setFont傳遞一個QFont
  2. 也嘗試通過其styleSheet的字體系列進行更改。

例如,在我的代碼,我只是做這個:

from PyQt5.QtCore import QUrl 
from PyQt5.QtGui import QFont 
from PyQt5.QtWebEngineWidgets import QWebEngineView 


class WebView(QWebEngineView): 


    def __init__(self): 
     super(WebView, self).__init__() 
     self.init_ui() 


    def init_ui(self): 
     self.load(QUrl("http://www.google.com/")) 
     self.setFont(QFont("Arial Black")) 

,它在Linux下的Windows,OSX工作得很好,即使是在樹莓。

+0

我很抱歉,但setFont不能在Linux上工作(至少對我而言)。我不知道爲什麼。是否因爲我使用setHtml來設置視圖的內容? – Rififi

+0

可能,即使我使用self ... load(...)我也使用setHtml,所以不知道爲什麼你有這個問題,對不起「/ – yurisnm

+0

好吧,我有更多的信息。在linux上,setFont但我仍然可以使用我在問題中展示的技巧,在Mac OS上,問題來自於'.NS SF Text''字體,我可以在任何其他字體上使用我的技巧,但這一個。我猜一些權限問題:https://eclecticlight.co/2015/11/10/qa-how-to-use-san-francisco-font-in-docs/。這個字體應該只用於奇怪的是,我可以在我的軟件中的任何地方使用它,除了在QWebEngineView中。 – Rififi