Using Fonts Awesome in QML

Hi,

Today i read this post on the planet and it made me think: “can’t we use that already? It’s just a font..”.

It actually is quite easy to get it working. All you need is QML and the ttf font. This is what you need in QML:

And that’s it. That allows you to use the quite nice monochrome icons. You obviously need to change the location to wherever you downloaded fontawesome-webfont.ttf. Which you can be found in the download package from FontsAwesome.

There are a few things to know here. First, take a look at the available icons. You can’t use the names described in that page. But you can use their unicode key which you can find here. Now if you see an icon that you, for example the “icon-ok”, you type on this page (just using CTRL + F). There you will see that the unicode key for “icon-ok” is: “\f00c“. Putting exactly that in QML won’t work. Unicode keys have to be prefixed with “\u” so in this case you simply add a “u” after the slash and you’re done. The results is: ”\uf00c“.

That’s it. Easy right :)

Cheers,
Mark

Update

Somehow the “FontAwesome.ttf” in the root of the download package from their site isn’t working with the way i described above. You need to use the “fontsawesome-webfont.ttf” which you can find in the “font” folder of that same package.

Update 2

Since some people don’t like putting in unicode values in the text value, which i can perfectly understand, i made a translation array. Save the following as “fontawesome.js”

Now in QML you can use it as follows:

In my case i named the array “fontawesome.js” which i then include in QML as “FontAwesome”. Then in the text part you can use it with FontAresome.Icon.<the icon you want>.

When naming all the icons i went for camelcase with the first char as capital as well. I couldn’t do “icon-ok” because a “-” isn’t allowed in variable names in javascript. Good luck using it!

10 Thoughts on “Using Fonts Awesome in QML

  1. We can also get the glyphs directly from the svg font and use them

    glyph unicode=”” d=”M0 732.5q0 33.5 23 55.5l174 175q23 23 56.5 22.5t55.5 -22.5l365 -365q23 -23 56.5 -23t55.5 23l746 745q23 23 56.5 23t56.5 -23l174 -174q23 -23 22.5 -56.5t-22.5 -55.5l-910 -910q-23 -23 -62.5 -39t-72.5 -16h-88q-35 0 -75 16.5t-62 38.5l-526 529 q-23 23 -23 56.5z”

  2. Hi.
    But… how do you deal with the fact that, a month from now, you will forget what the hell the character ‘\uf00c’ means?

  3. /// Does not work :(

    import QtQuick 2.0

    Rectangle {
    width: 320; height: 480; color: “grey”

    ListModel{
    id:id_elements
    }

    function dec2hex(i)
    {
    var result = “0000″;
    if (i >= 0 && i = 16 && i = 256 && i = 4096 && i <= 65535) { result = i.toString(16); }
    result = result.substring(2,4);

    return result
    }
    Component.onCompleted: {
    var sym = "\f0";
    for (var j=0;j <= 234; j++){
    id_elements.append({"value":"f0"+dec2hex(j)});
    }
    id_elements.append({"value":"f200"});
    }
    FontLoader{id:id_font_loader;source: "FontAwesome.ttf"}
    GridView {
    anchors.fill: parent
    model: id_elements

    delegate: Rectangle{
    width:40//Math.max(childrenRect.width,18)
    height:40//Math.max(childrenRect.height,18)
    border.width: 1
    border.color:"black"
    color:"lightgrey"
    Text {
    id:id_text
    anchors.centerIn: parent
    text: "\u"+value
    font.family: id_font_loader.name
    font.pixelSize: 30
    color: "black"
    }
    Component.onCompleted: print(value)
    }
    }
    }

  4. jstaniek on November 21, 2012 at 10:43 AM said:

    Code like the one presented can work and while quite large it’s still smaller than the spaghetti of CSS/HTML hacks used on the web. This approach is apparently used by Jeremy in his app (https://qtconference.kdab.com/node/23#Serving_QML). But of course the goal was to have easy to use QML component, with icons accessible by name, and it would be best to have option for using the freedesktop.org naming.

    Also I am unsure of performance penalty, when using font loader directly in every UI element that needs an icon; this has to be benchmarked.

    • markg85 on November 21, 2012 at 1:54 PM said:

      I don’t think you need to worry about the performance. It are just “fonts” after all. Fonts (as in the stuff in Qt that loads the fonts) is quite optimized for performance. So i really don’t think you’d want to have a layer in between to “optimize” it further. First try to figure out “if” it even needs optimizing.

      As for calling it by name. You can just put the unicode values in javascript variables.. That would make it readable but would also add a quite big list of variables.

  5. does this work in QtQuick 2.0 ?

    • markg85 on November 21, 2012 at 1:55 PM said:

      It should. I’m not using anything special in my example so the example should just work (if you change the font path obviously)

  6. markg85 on November 21, 2012 at 2:03 PM said:

    Heads up, be sure to read the update in my post. That will likely also fix the issue “smoggy” has :)

  7. markg85 on November 21, 2012 at 4:12 PM said:

    Second heads up. Just read the second update in the main post.

  8. mark g. number 85 :) thanks for the update, it worked fine for me with s/QtQuick 1.1/QtQuick 2.0/g
    My example still does not work, dunno why; maybe the concat of \uf0XX does not work properly.
    I’ll just use your JS library when i need any character.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Post Navigation