如何使用样式表的非标准自定义字体? [英] How to use Non-Standard Custom Font with Stylesheets?
问题描述
我有一个 PyQt4
应用程序正在使用下面的代码由外部 .qss
文件进行样式化:
...
app = QtGui.QApplication(sys.argv)
stylesheet = open(' ())
app.setStyleSheet(stylesheet)
...
正常情况下,我会在 .qss
文件中指定我喜欢的字体类型,如下所示:
QMainWindow
{
font-family:arial;
font-size:14px;
$ b 但是,现在我想知道是否可以为我分配一个自定义字体,我从互联网上下载(例如, DroidSansMono (真正的字体)),而不是Windows标准字体?
$ b
我使用Windows XP SP3 32位,Python 2.7
更新1:
Ekhumoro答案:
在加载样式表
之前,我可以使用下载的自定义字体将其添加到字体数据库中:
QtGui.QFontDatabase.addApplicationFont(Resources / Mf Wedding Bells.ttf)
之后,我可以简单地使用刚才在样式表中添加的字体名称:
QLabel
{
font-family:Mf Wedding Bells;
font-size:16px;
}
解决方案这只是一个猜测,因为我自己无法测试,但是您可以尝试在设置样式表之前加载字体:
pre> app = QtGui.QApplication(sys.argv)
QtGui.QFontDatabase.addApplicationFont('path / to / font')
#或直接加载字体数据
#QtGui.QFontDatabase.addApplicationFontFromData(fontdata)
stylesheet = open('mystylesheet.qss')。read()
app.setStyleSheet(stylesheet)
I have a PyQt4
application that is being styled by an external .qss
file by using the following code:
...
app = QtGui.QApplication(sys.argv)
stylesheet = open('mystylesheet.qss').read()
app.setStyleSheet(stylesheet)
...
Normally, I would specify the type of font that I like in the .qss
file to use like this:
QMainWindow
{
font-family:arial;
font-size:14px;
}
But, now I am wondering if it is possible for me to assign a custom font that I downloaded from internet (example, DroidSansMono (True Type Font) ) instead of windows standard font?
NOTE: I am using Windows XP SP3 32 bits, with Python 2.7
UPDATE 1:
Based on Ekhumoro answer:
I can use the custom font downloaded by adding it to the font database before loading the Stylesheet
:
QtGui.QFontDatabase.addApplicationFont("Resources/Mf Wedding Bells.ttf")
After that, I can simply use the font name that I have just added in the stylesheet like this:
QLabel
{
font-family:Mf Wedding Bells;
font-size:16px;
}
And it works!!!
解决方案 This is just a guess, because I cannot test it myself, but you could try loading 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)
这篇关于如何使用样式表的非标准自定义字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!