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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import QtQuick 1.1 Rectangle { width: 800 height: 600 // Load the "FontAwesome" font for the monochrome icons. FontLoader { source: "fonts/fontawesome-webfont.ttf" } Text { anchors.centerIn: parent font.pointSize: 250 font.family: "FontAwesome" text: "\uf00c" } } |
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”
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
var Icon = { Glass : "\uf000", Music : "\uf001", Search : "\uf002", Envelope : "\uf003", Heart : "\uf004", Star : "\uf005", StarEmpty : "\uf006", User : "\uf007", Film : "\uf008", ThLarge : "\uf009", Th : "\uf00a", ThList : "\uf00b", Ok : "\uf00c", Remove : "\uf00d", ZoomIn : "\uf00e", ZoomOut : "\uf010", Off : "\uf011", Signal : "\uf012", Cog : "\uf013", Trash : "\uf014", Home : "\uf015", File : "\uf016", Time : "\uf017", Road : "\uf018", DownloadAlt : "\uf019", Download : "\uf01a", Upload : "\uf01b", Inbox : "\uf01c", PlayCircle : "\uf01d", Repeat : "\uf01e", Refresh : "\uf021", ListAlt : "\uf022", Lock : "\uf023", Flag : "\uf024", Headphones : "\uf025", VolumeOff : "\uf026", VolumeDown : "\uf027", VolumeUp : "\uf028", Qrcode : "\uf029", Barcode : "\uf02a", Tag : "\uf02b", Tags : "\uf02c", Book : "\uf02d", Bookmark : "\uf02e", Print : "\uf02f", Camera : "\uf030", Font : "\uf031", Bold : "\uf032", Italic : "\uf033", TextHeight : "\uf034", TextWidth : "\uf035", AlignLeft : "\uf036", AlignCenter : "\uf037", AlignRight : "\uf038", AlignJustify : "\uf039", List : "\uf03a", IndentLeft : "\uf03b", IndentRight : "\uf03c", FacetimeVideo : "\uf03d", Picture : "\uf03e", Pencil : "\uf040", MapMarker : "\uf041", Adjust : "\uf042", Tint : "\uf043", Edit : "\uf044", Share : "\uf045", Check : "\uf046", Move : "\uf047", StepBackward : "\uf048", StepForward : "\uf049", Backward : "\uf04a", Play : "\uf04b", Pause : "\uf04c", Stop : "\uf04d", Forward : "\uf04e", FastForward : "\uf050", StepForward : "\uf051", Eject : "\uf052", ChevronLeft : "\uf053", ChevronRight : "\uf054", PlusSign : "\uf055", MinusSign : "\uf056", RemoveSign : "\uf057", OkSign : "\uf058", QuestionSign : "\uf059", InfoSign : "\uf05a", Screenshot : "\uf05b", RemoveCircle : "\uf05c", OkCircle : "\uf05d", BanCircle : "\uf05e", ArrowLeft : "\uf060", ArrowRight : "\uf061", ArrowUp : "\uf062", ArrowDown : "\uf063", ShareAlt : "\uf064", ResizeFull : "\uf065", ResizeSmall : "\uf066", Plus : "\uf067", Minus : "\uf068", Asterish : "\uf069", ExclamationSign : "\uf06a", Gift : "\uf06b", Leave : "\uf06c", Fire : "\uf06d", EyeOpen : "\uf06e", EyeClose : "\uf070", WarningSign : "\uf071", Plane : "\uf072", Calendar : "\uf073", Random : "\uf074", Comment : "\uf075", Magnet : "\uf076", ChevronUp : "\uf077", ChevronDown : "\uf078", Retweet : "\uf079", ShoppingCart : "\uf07a", FolderClose : "\uf07b", FolderOpen : "\uf07c", ResizeVertical : "\uf07d", ResizeHorizontal : "\uf07e", BarChart : "\uf080", TwitterSign : "\uf081", FacebookSign : "\uf082", CameraRetro : "\uf083", Key : "\uf084", Cogs : "\uf085", Comments : "\uf086", ThumbsUp : "\uf087", ThumbsDown : "\uf088", StarHalf : "\uf089", HeartEmpty : "\uf08a", Signout : "\uf08b", LinkedinSign : "\uf08c", Pushpin : "\uf08d", ExternalLink : "\uf08e", Signin : "\uf090", Trophy : "\uf091", GithubSign : "\uf092", UploadAlt : "\uf093", Lemon : "\uf094", Phone : "\uf095", CheckEmpty : "\uf096", BookmarkEmpty : "\uf097", PhoneSign : "\uf098", Twitter : "\uf099", Facebook : "\uf09a", Github : "\uf09b", Unlock : "\uf09c", CreditCard : "\uf09d", Rss : "\uf09e", Hdd : "\uf0a0", Bullhorn : "\uf0a1", Bell : "\uf0a2", Certificate : "\uf0a3", HandRight : "\uf0a4", HandLeft : "\uf0a5", HandUp : "\uf0a6", HandDown : "\uf0a7", CircleArrowLeft : "\uf0a8", CircleArrowRight : "\uf0a9", CircleArrowUp : "\uf0aa", CircleArrowDown : "\uf0ab", Globe : "\uf0ac", Wrench : "\uf0ad", Tasks : "\uf0ae", Filter : "\uf0b0", Briefcase : "\uf0b1", Fullscreen : "\uf0b2", Group : "\uf0c0", Link : "\uf0c1", Cloud : "\uf0c2", Beaker : "\uf0c3", Cut : "\uf0c4", Copy : "\uf0c5", PaperClip : "\uf0c6", Save : "\uf0c7", SignBlank : "\uf0c8", Reorder : "\uf0c9", ListUl : "\uf0ca", ListOl : "\uf0cb", Strikethrough : "\uf0cc", Underline : "\uf0cd", Table : "\uf0ce", Magic : "\uf0d0", Truck : "\uf0d1", Pinterest : "\uf0d2", PinterestSign : "\uf0d3", GooglePlusSign : "\uf0d4", GooglePlus : "\uf0d5", Money : "\uf0d6", CaretDown : "\uf0d7", CaretUp : "\uf0d8", CaretLeft : "\uf0d9", CaretRight : "\uf0da", Columns : "\uf0db", Sort : "\uf0dc", SortDown : "\uf0dd", SortUp : "\uf0de", EnvelopeAlt : "\uf0e0", Linkedin : "\uf0e1", Undo : "\uf0e2", Legal : "\uf0e3", Dashboard : "\uf0e4", CommentAlt : "\uf0e5", CommentsAlt : "\uf0e6", Bolt : "\uf0e7", Sitemap : "\uf0e8", Unbrella : "\uf0e9", Paste : "\uf0ea", UserMd : "\uf200", }; |
Now in QML you can use it as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import QtQuick 1.1 import "fontawesome.js" as FontAwesome Item { width: 800 height: 600 // Load the "FontAwesome" font for the monochrome icons. FontLoader { source: "fontawesome-webfont.ttf" } Text { anchors.centerIn: parent font.pointSize: 250 font.family: "FontAwesome" text: FontAwesome.Icon.Ok } } |
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!

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”
Hi.
But… how do you deal with the fact that, a month from now, you will forget what the hell the character ‘\uf00c’ means?
/// 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)
}
}
}
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.
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.
does this work in QtQuick 2.0 ?
It should. I’m not using anything special in my example so the example should just work (if you change the font path obviously)
Heads up, be sure to read the update in my post. That will likely also fix the issue “smoggy” has :)
Second heads up. Just read the second update in the main post.
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.