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
}
Related
my code
Text {
Layout.topMargin: 5
Layout.preferredWidth: parent.width
Layout.preferredHeight: columnsItem.titleHeight
font: Style.h4
wrapMode: Text.Wrap
maximumLineCount: 2
elide: Text.ElideRight
lineHeight: 1.5
text: BodyViewModelCpp.findMusicPageColumns.getSubitemProperty(
modelData, "title")
}
I want to let left "..." look like the right hand side one.
They are both marked using red rectangle in following screen shot
I think you want to replace the Chinese ellipsis with an alternative right? We can use TextMetrics to determine the elidedText and we can suppress or replace Chinese ellipsis before it is shown in the Text component:
import QtQuick
import QtQuick.Controls
Page {
Frame {
width: 200
Text {
id: itemLabel
width: parent.width
property TextMetrics textMetrics: TextMetrics {
text: "The quick brown fox jumped over the lazy dog"
elideWidth: itemLabel.width
elide: Text.ElideRight
}
text: textMetrics.elidedText.replace(/…$/,"...")
}
}
}
You can Try it Online!
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
I am developing a Qt program using QML in which a user has to enter a variable-sized input (up to 50 chars). Since the program window is not big enough I cannot accommodate a 50 char-wide input rectangle. I would like the input box ("inputNameField" below) to act as a mask over the text so that the characters that are out of the input box are not visible. Here is my base code:
Rectangle
{
id: inputNameBox
onVisibleChanged: if (visible) textNameInput.forceActiveFocus()
anchors.verticalCenter: parent.verticalCenter
anchors.horizontalCenter: parent.horizontalCenter
color: 'grey'
radius: 5
height: parent.height/8
width: parent.width/4
TextInput
{
id: textNameInput
autoScroll: true
anchors.margins: inputNameBox.radius
anchors.fill: inputNameBox
font.pixelSize: inputNameBox.height/2
maximumLength: 50
horizontalAlignment: TextInput.AlignHCenter
verticalAlignment: TextInput.AlignVCenter
}
}
I have tried using the inputNameField as an OpacityMask over the textNameInput to no avail.
EDIT: duplicate of Custom TextEdit, how to hide TextInput when it goes wide
Consider this simple code sample:
import QtQuick 2.5
import QtQuick.Window 2.2
Window {
visible: true
color: "black"
Text {
text: "Hello World!"
font.family: "Helvetica"
font.pointSize: 24
color: "red"
}
}
Why is there a margin at the top? It seems that giving a custom font with a specific pixelSize gives a new size to the Text and breaks the alignement.
EDIT: well it seems that even without font there still is a margin.
A simpler solution would be to set the attribute y of the Text like this:
Text {
text: "Hello World!"
font.family: "Helvetica"
font.pixelSize: 42
color: "red"
y: -contentHeight + font.pixelSize
}
Bear in mind that if you set the property font.overline: true then it will be out of the Rectangle
I managed to remove this extra space with the use of FontMetrics but this solution seems way too complicated for this simple problem. Waiting for a better fix.
Window {
visible: true
color: "black"
FontMetrics {
id: fontmetrics24
font.pixelSize: 24
font.family: "Helvetica"
}
Text {
text: "Hello World!"
font.family: fontmetrics24.font.family
font.pointSize: fontmetrics24.font.pixelSize
color: "red"
y: - fontmetrics24.height * 0.21
}
}
I think this issue is related to this question. On macOS (Sierra), the window looks like this:
As ddriver pointed out, this is probably a bug and is already reported here.
I've been having trouble with different OSs (tried Windows 7 and Ubuntu 13.10) having different font widths and heights. I tried setting pixelSize and pointSize and both results in different sizes in both Arial and Courier font texts (Windows fonts are generally bigger).
Here is an example:
Rectangle {
width: 312
height: 44
Text {
id: text_1
text: qsTr("1234567890123456789012345678901234567890")
anchors.top: parent.top
anchors.left: parent.left
anchors.right: parent.right
font.family: "Courier"
font.pointSize: 10
}
Text {
id: text_2
text: qsTr("123456789012345678901234567890")
anchors.top: text_1.bottom
anchors.left: parent.left
anchors.right: parent.right
font.family: "Courier"
font.pointSize: 10
}
Text {
id: text_3
text: qsTr("12345678901234567890123456789012345")
anchors.top: text_2.bottom
anchors.left: parent.left
anchors.right: parent.right
font.family: "Courier"
font.pointSize: 10
}
}
This rectangle's width and height are defined by trial and error to fit the 3 text's in Ubuntu. See screenshots below:
In my application, I'd like one text object (with Courier font) to fill it's parent's width (text is fixed). In another rectangle, I'd like the text objects (more than one, anchored just like in the example above, with Arial font) to fill the parent's height.
Currently, I'm looking for a way to set the parent's width and height dynamically, calculated from the fixed text content, but I can't shake the feeling there's got to be a simpler way to do this.
Any tips are appreciated.
Choosing a parent height/width in this case in a bad idea - as you can see, it's not portable; and what if you decided to change your font later, would you want to recalculate everything? This is something you want to delegate to QML.
I suggest using Layouts, like this :
import QtQuick.Layouts 1.1 // Necessary to use ColumnLayout
ColumnLayout { // A Single Column
Text {
id: text_1
text: qsTr("1234567890123456789012345678901234567890")
// No need for anchoring ! You may want to use Layout.fillWidth: true in some cases
font.family: "Courier"
font.pointSize: 10
}
Text {
id: text_2
text: qsTr("123456789012345678901234567890")
font.family: "Courier"
font.pointSize: 10
}
Text {
id: text_3
text: qsTr("12345678901234567890123456789012345")
font.family: "Courier"
font.pointSize: 10
}
}