Qt World Summit: Register Today!

Get the style of the font file using QFont

  • Is there a command in QFont where I can get the style of the font. I figured out how to get the family name of the font file but not the specific style of the file. I found a command which returns the entire list of styles associated with the font family.

    styleList = QtGui.QFontDatabase.styles(fontFamilyName)

    I need the exact style of the specific font file. For example, if I give arialb.ttf as an input, I should be getting family name: Arial; style: Bold. Any ideas?

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Do you mean QFont::styleName ?

  • Hello Gaist,

    Thanks for the quick response! I tried this but I am getting empty string. This is my code

           fontFile = "C:\Users\Desktop\abc.otf"
           fontdb = QtGui.QFontDatabase()
           id = fontdb.addApplicationFont(fontFile)
           family = fontdb.applicationFontFamiles(id)
           font = QtGui.QFont(family[0])
           print font.styleName()

    What am I doing wrong?

  • Lifetime Qt Champion

    So it seems you're using the one desktop OS that isn't supported by this method

    However, depending on what information you are looking for, Weight might be an option

  • Weight command is also not working. I'm always getting this as output no matter what the font is


  • Lifetime Qt Champion

    Weight is an enumeration and theres a corresponding getter

Log in to reply