QML not using Thin font weight

戏子无情 提交于 2020-01-02 07:04:42

问题


I have a QML app using the freely-available Encode Sans font, which comes in 9 weights that match Qt 5.6's font weights.

I have added all .ttf to my project, and am using a FontLoader to load them. Everything works perfectly, except for the Thin weight of the font.

In addition to the code below I have tried providing a unique id on the FontLoader for Thin, to ensure that the family name is the same (it is). How can I debug this problem and get Thin working?

### main.qml
FontLoader { source:"qrc:/fonts/encodesans/EncodeSans-Thin.ttf" }
FontLoader { source:"qrc:/fonts/encodesans/EncodeSans-ExtraLight.ttf" }
FontLoader { source:"qrc:/fonts/encodesans/EncodeSans-Light.ttf" }
FontLoader { id:mainFont; source:"qrc:/fonts/encodesans/EncodeSans-Regular.ttf" }
FontLoader { source:"qrc:/fonts/encodesans/EncodeSans-Medium.ttf" }
FontLoader { source:"qrc:/fonts/encodesans/EncodeSans-SemiBold.ttf" }
FontLoader { source:"qrc:/fonts/encodesans/EncodeSans-Bold.ttf" }
FontLoader { source:"qrc:/fonts/encodesans/EncodeSans-ExtraBold.ttf" }
FontLoader { source:"qrc:/fonts/encodesans/EncodeSans-Black.ttf" }

Column {
  TextLine { weight:"Thin"       }
  TextLine { weight:"ExtraLight" }
  TextLine { weight:"Light"      }
  TextLine { weight:"Normal"     }
  TextLine { weight:"Medium"     }
  TextLine { weight:"DemiBold"   }
  TextLine { weight:"Bold"       }
  TextLine { weight:"ExtraBold"  }
  TextLine { weight:"Black"      }
}
### TextLine.qml
import QtQuick 2.0
Text {
  property string weight

  text: "1 2 3 4 font.weight: Font."+weight
  color:"white"
  font {
    weight: Font[weight]
    family: mainFont.name
    pixelSize: 36
  }
}

Using Qt 5.6 on Ubuntu, if it matters.


回答1:


This is a Qt bug : [QTBUG-53196] Thin fonts do not work in Qt

One workaround you could use is using the styleName property instead wich was introduced in Qt 5.6 . The disadvantage of this method is that if you have some embedded bold in your text (with html or rich text), it won't work (if I understand this bug correctly).

You can use it like this :

### TextLine.qml
import QtQuick 2.0
Text {
  property string weight

  text: "1 2 3 4 font.weight: Font."+weight
  color:"white"
  font {
    styleName: weight
    family: mainFont.name
    pixelSize: 36
  }
}


来源:https://stackoverflow.com/questions/37573143/qml-not-using-thin-font-weight

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!