How to use custom font with styles?

I have a PyQt4 application that is created by an external .qss file using the following code:

 ... app = QtGui.QApplication(sys.argv) stylesheet = open('mystylesheet.qss').read() app.setStyleSheet(stylesheet) ... 

Normally, I would like to specify the type of font that I like in the .qss file, for use as follows:

 QMainWindow { font-family:arial; font-size:14px; } 

But now I'm wondering, can I assign a custom font that I downloaded from the Internet (e.g. DroidSansMono (True Type Font)) instead of the standard Windows font?

NOTE. I am using 32 bit Windows XP SP3, with Python 2.7

UPDATE 1:

Based on Ehumoro's answer:

I can use a custom font downloaded by adding it to the font database before loading Stylesheet :

 QtGui.QFontDatabase.addApplicationFont("Resources/Mf Wedding Bells.ttf") 

After that, I can simply use the font name that I just added in the stylesheet, for example:

 QLabel { font-family:Mf Wedding Bells; font-size:16px; } 

And it works !!!

+6
source share
1 answer

This is just an assumption because I cannot verify it myself, but you can try downloading the font before setting the stylesheet:

 app = QtGui.QApplication(sys.argv) QtGui.QFontDatabase.addApplicationFont('path/to/font') # or load the font data directly # QtGui.QFontDatabase.addApplicationFontFromData(fontdata) stylesheet = open('mystylesheet.qss').read() app.setStyleSheet(stylesheet) 
+7
source

Source: https://habr.com/ru/post/981031/


All Articles