【问题标题】:How to use Non-Standard Custom Font with Stylesheets?如何在样式表中使用非标准自定义字体?
【发布时间】:2015-03-13 09:17:23
【问题描述】:

我有一个 PyQt4 应用程序,它使用以下代码由外部 .qss 文件设置样式:

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

通常,我会在 .qss 文件中指定我喜欢的字体类型,以便像这样使用:

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

但是,现在我想知道是否可以分配我从互联网下载的自定义字体(例如,DroidSansMono (True Type Font))而不是 Windows 标准字体?

注意:我使用的是 Windows XP SP3 32 位和 Python 2.7

更新 1:

基于 Ekhumoro 的回答:

我可以使用下载的自定义字体,方法是在加载Stylesheet之前将其添加到字体数据库中:

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

之后,我可以像这样简单地使用我刚刚在样式表中添加的字体名称:

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

它的工作原理!

【问题讨论】:

    标签: python fonts pyqt pyqt4 qtstylesheets


    【解决方案1】:

    这只是一个猜测,因为我自己无法测试,但您可以在设置样式表之前尝试loading the font:

    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)
    

    【讨论】:

      猜你喜欢
      • 2013-03-07
      • 1970-01-01
      • 2019-02-07
      • 1970-01-01
      • 2015-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多