Only some Font Awesome 5 icons work in Qt Qml - qt

I am trying to show Font Aweseom 5 (5.10.2) icons in a Qt Qml label:
Label {
text: "\uf2bb" + " x " + "\uf002"
font.family: "Font Awesome 5 Free"
//font.weight: Font.Normal
}
On Windows all icons work as expected. But on all other platforms (macOS, Android, iOS) only some icons (for instance \uf2bb) are shown while others (\uf4b4) do not work and a simple rectangle is shown instead. But all icons work on all platforms when QWidgets are used.
I verified that the "Font Awesome 5 Free" font is installed in the QFontDatabase using this utility list view:
ListView {
anchors.fill: parent;
model: Qt.fontFamilies()
delegate: Item {
height: 40;
width: ListView.view.width
Label {
anchors.centerIn: parent
text: modelData;
}
}
}
Has anyone an idea why some icons work in Qml, while others don't?
Regards,

Either setting the font weight to Font.Black or the font's style name to "Solid" fixes the problem:
Label {
text: "\uf2bb" + " x " + "\uf002"
font.family: "Font Awesome 5 Free"
font.weight: Font.Black // this does the trick
}
Label {
text: "\uf2bb" + " x " + "\uf002"
font.family: "Font Awesome 5 Free"
font.styleName: "Solid" // this does the trick
}

I fixed this problem with https://fontforge.org/
On file fa-solid-900.ttf I modified the field WWS Family (under tab TTF Names) from Font Awesome 5 Free to Font Awesome 5 Free Solid
Maybe Qt for Android uses this field and without modification the value is the same for fa-regular-400.ttf and fa-solid-900.ttf
Hope it helps

Related

QML set font capitalization

I'm writing a reusable font property, as described here and here.
Here's my property
property font button2: Qt.font({
family: defaultFont.name,
styleName: "Bold",
pointSize: 16
})
and here it how I use it, and it works fine
Text {
text: "button"
font: MyFont.button2
}
Now I would add the capitalization but adding it to the property like this
property font button2: Qt.font({
family: defaultFont.name,
styleName: "Bold",
capitalization: "AllUppercase",
pointSize: 16
})
It does not have effect at all. If I add it in the text component instead
Text {
text: "button"
font: MyFont.button2
font.capitalization: Font.AllUppercase
}
it gives me a "Property has already been assigned a value" error.
How should I do that?
If you want to change the capitalization then you must use it in Component.onCompleted:
Text {
text: "button"
font: MyFont.button2
// font.capitalization: Font.AllUppercase
Component.onCompleted: font.capitalization = Font.AllUppercase
}

Qt QML fails to render text correctly on some systems

I have so far seen this issue on a Win 7 PC, not sure if it will happen on all Win 7 PCs?
As you can see, the text 'Personal Info' defined at the top inside a QML Text{..} box is incorrectly rendered:
I realized that the text below the header was working fine, this is defined as a Label{...}, so I replaced the Text object with a Label object recompiled and ran on the target machine and it now works -
The code is compiled with Qt 5.10.1 minGW 32bit.
The qml file is fairly long, the area in question is as follows:
Rectangle{
id:titleNameContainer
color: "transparent"
anchors.top: parent.top
width: parent.width
height: 40
anchors.horizontalCenter: parent.horizontalCenter
CenturyGothicFont{id: nameStrFont}
Label{
id: nameStr
text: "Personal Info"
width:parent.width
//color: "black"
horizontalAlignment: Text.AlignHCenter
font{
family: nameStrFont.name
pixelSize: 30
}
}
}
The 'label' was a 'text' object.
The text itself is an imported font of type .ttf, this font clearly works when used as a label. I have not made any changes to the anti aliasing settings as asked in the comments (I would have to look this up if I wanted to / need to).
Can anyone explain what is going on here?

Using font awesome in QML: wrong characters

I would like to use font awesome font in my QML app. But it doesn't work very well.
First I use this method : https://github.com/QMLCommunity/font-awesome-qml
But I got only two icons working, see the capture of my app.
Only the laptop and battery icons are displayed. The others (one top left, and others on each tab) got kanjis or other symbols.
After I also tried that solution : How to use Font Awesome in QML
on the top left icon, whithout success.
Here the code of the notification bar :
RowLayout {
anchors.fill: parent
Label {
font.pointSize: 10
font.family: "fa-solid-900"
text: "\uf071" + " Message"
}
Item {
Layout.fillWidth: true
}
Label {
font.pointSize: 10
font.family: awesome.family
text: awesome.loaded ? awesome.icons.fa_laptop : "x"
Layout.alignment: Qt.AlignRight
}
Label {
font.pointSize: 10
font.family: awesome.family
text: awesome.loaded ? awesome.icons.fa_battery_full + " 100 %" : "x"
Layout.alignment: Qt.AlignRight
}
}
PS: I used the 2nd link solution for the first icon, but the result is exactly the same with the first solution.
And the tab bar:
footer: TabBar {
id: tabBar
currentIndex: swipeView.currentIndex
Material.theme: Material.Dark
FontAwesome {
id: awesome
resource: "qrc:///controls/fa-solid-900.ttf"
}
TabButton {
font.pointSize: 10
font.family: awesome.family
text: (awesome.loaded ? awesome.icons.fa_star : "x") + " " + "Général"
}
TabButton {
font.pointSize: 10
font.family: awesome.family
text: (awesome.loaded ? awesome.icons.fa_cogs : "x") + " " + "Test"
}
TabButton {
font.pointSize: 10
font.family: awesome.family
text: (awesome.loaded ? awesome.icons.fa_trophy : "x") + " " + "Match"
}
}
How can I display the right icons each time?
Thanks in advance!
I recently ran into the same issues and documented my approach in a blog post.
Basically, my issue was that starting with version 5, Font Awesome provides some of the icons in one font file with "regular" weight and one in a file with "solid" weight classes. However, when loading, both will register with a font name of "Font Awesome 5 Free" (assuming the free version is used).
I would have expected that it is possible to switch between the regular and solid icon sets by setting e.g. the bold property of a font in QML:
import QtQuick 2.0
Item {
width: 100
height: 100
FontLoader {
id: faRegular
source: "./fa-regular-400.ttf"
}
FontLoader {
id: faSolid
source: "./fa-solid-900.ttf"
}
Column {
width: parent.width
Text {
font.family: "Font Awesome 5 Free"
font.bold: true // use "solid" variant
text: "\uf073" // fa-calendar-alt
}
Text {
font.family: "Font Awesome 5 Free"
font.bold: false // use "regular" variant
text: "\uf073" // fa-calendar-alt
}
}
}
Unfortunately, this did not work for me, as Qt merges both fonts and only either the regular or the solid variant was used (which causes some icons from not being rendered at all, especially if you use the regular variant which contains much less icons at least in the free version of the font).
My "solution" was to edit the solid font file (e.g. using FontForge) and change the font name to e.g. "Font Awesome 5 Free Solid". With this, I was able to load both variants of the font at the same time and switch between the two by using their font name like this:
import QtQuick 2.0
Item {
width: 100
height: 100
FontLoader {
id: faRegular
source: "./fa-regular-400.ttf"
}
FontLoader {
id: faSolid
source: "./fa-solid-900.ttf"
}
Column {
width: parent.width
Text {
font.family: faSolid.name // use solid variant
text: "\uf073" // fa-calendar-alt
}
Text {
font.family: faRegular.name // use regular variant
text: "\uf073" // fa-calendar-alt
}
}
}
This is probably due to font merging when some characters cannot be resolved. You can try to load FontAwesome from C++ side
Loading the font with a QFontDatabase
int fontId = QFontDatabase::addApplicationFont(QStringLiteral("/path/to/font-awesome.ttf");
Disabling merging on a new font
QFont font("fa-solid-900"); // You can also use the fontId to get family name here
font.setStyleStrategy(QFont::NoFontMerging);
Exposing the font to QML with a QFont Q_PROPERTY or directly in the context
Q_PROPERTY(QFont fontAwesome READ fontAwesome CONSTANT)
This is discussed in the Qt mailing list here

Qt QML font awesome not working on MAC and IOS

I am working on QML and loading the font awesome using FontLoader
FontLoader {
id: awesomeLoader
name: "awesome"
source: "qrc:/images/fontawesome-webfont.ttf"
}
Everything just find on Linux and Android, but it is not working on IOS and MAC, the font icon become empty rectangle.
This is how I use it :
Text {
font.pointSize: 20
font.family: "awesome"
anchors.fill: parent
text: isBackButton ? "\uf053" : "\uf0c9"
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignHCenter
}
Is there anything special on MAC / IOS?
Make sure you're setting font.family:
font.family: awesomeLoader.name
One thing I noticed on Windows was that I could get icons to show up without setting font.family: "FontAwesome" (I forgot to do it), but when switching to macOS they would be broken.
It works in this way:
FontLoader {
id: awesomeLoader
//name: "awesome" (Comment or delete)
source: "qrc:/images/fontawesome-webfont.ttf"
}
In Text Item change font.family value to awesomeLoader.name (FontLoader id name)
Text {
font.pointSize: 20
font.family: awesomeLoader.name
anchors.fill: parent
text: isBackButton ? "\uf053" : "\uf0c9"
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignHCenter
}

How to find out which fonts are available in qml?

I would like to know which fonts I can use in a QML environment for the font.family property. Are these fonts system-specific or are they built into the framework? Is there any way to list all available fonts?
This code will list all the accepted font families:
ListView {
anchors.fill: parent;
model: Qt.fontFamilies()
delegate: Item {
height: 40;
width: ListView.view.width
Text {
anchors.centerIn: parent
text: modelData;
}
}
}
The fonts are system-specific so you should see what your system proposes.
If you are using QtCreator :
try putting your mouse over the end of your component name
Text <here> {
...
}
You should see a yellow light, click on it and you'll have an interface that allows to choose the font.
You can also access the interface with ctrl + alt + space while inside the component. Or with right click.
This is a system-specific list of fonts, but you can specify external font from resources (QRC)
You can improve the previous answer by adding this
Text {
font.family: modelData
anchors.centerIn: parent
text: modelData;
}

Resources