首页 文章

QML不使用Thin字体粗细

提问于
浏览
1

我有一个使用免费提供的Encode Sans font的QML应用程序,它有9个与Qt 5.6's font weights匹配的权重 .

我已将所有.ttf添加到我的项目中,并使用 FontLoader 加载它们 . 除了字体的轻薄外,一切都很完美 .

除了下面的代码,我还尝试在FontLoader for Thin上提供一个唯一的 id ,以确保姓氏相同(它是) . 如何调试此问题并使Thin工作?

Screenshot of all weights being used. The Thin weight is listed first, and shows the same result as the Normal weight. All other weights vary as expected and look correct.

### 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
  }
}

如果重要的话,在Ubuntu上使用Qt 5.6 .

1 回答

  • 2

    这是一个Qt错误:[QTBUG-53196] Thin fonts do not work in Qt

    您可以使用的一种解决方法是使用styleName property而不是Qt 5.6中引入的 . 这种方法的缺点是,如果你的文本中有一些嵌入的粗体(使用html或富文本),它将无法正常工作(如果我正确理解这个bug) .

    你可以像这样使用它:

    ### 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
      }
    }
    

相关问题