Unable to use ttf icon font in QML - qt

I would like to use an icon font with QML and so I downloaded the great Line Awesome, but when I try to load the file and use it in QML it doesn't work. I am using FontLoader and then set the unicode string of the icon I want to the the text property of a Text element. In the code below when the text cannot interpret the unicode string (copied from the Line Awesome website) correctly.
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.12
Window {
id: window
visible: true
width: 1000
height: 700
title: qsTr("Hello World")
FontLoader{
id: f
source: "file:///home/user/Downloads/1.3.0/fonts/la-regular-400.ttf"
}
Text {
id: name
font.family: f.name
text: ""
}
Button{
anchors.right: parent.right
onClicked: console.debug(f.status == FontLoader.Ready, f.name) // returns true and "la-regular-400"
}
}

The only way I know of that makes it work reliably is:
Load the font using QFontDatabase, during application startup or at some other convenient time.
Use the QFontInfo to get the actual family name of that font as seen by Qt. What confuses most people is that QFont is a bit of a misnomer. It's not a font at all, it's just a request for a font. The actual font's information is provided by QFontInfo.
Use that family name in Qt Quick and elsewhere.

Related

How to change triangle in ComboBox qml?

How do I change the size and color of the triangle in a ComboBox? And also flip the triangle?
Now it looks like this
I'm assuming you're using the Material style.
Without declaring your own indicator:
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
Window {
id: window
width: 640
height: 480
visible: true
ComboBox {
id: comboBox
model: 10
Binding {
target: comboBox.indicator
property: "rotation"
value: 180
}
Binding {
target: comboBox.indicator
property: "color"
value: "tomato"
}
}
}
Declaring your own indicator:
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
Window {
id: window
width: 640
height: 480
visible: true
ComboBox {
id: comboBox
model: 10
indicator: ColorImage {
x: comboBox.mirrored ? comboBox.padding : comboBox.width - width - comboBox.padding
y: comboBox.topPadding + (comboBox.availableHeight - height) / 2
color: "tomato"
rotation: 180
source: "qrc:/qt-project.org/imports/QtQuick/Controls/Material/images/drop-indicator.png"
}
}
}
Both approaches assume something about the QML implementation of the style in use:
The first approach assumes that the indicator has a color property. This could change in a future version (although it's very unlikely).
The second approach uses an internal resource URL (for convenience, since it's an image that everyone testing this code should have available on their machines), but I wouldn't generally encourage doing so yourself. If you're sure that your application will use the Material style, then it should be fine, but again, this path could change in a future version. If you want a more future-proof option, use your own image for the indicator.
If you want a completely future-proof option, implement your own style:
https://doc.qt.io/qt-5/qtquickcontrols2-customize.html#definition-of-a-style
Qt 6 version of that page:
https://doc.qt.io/qt-6/qtquickcontrols2-customize.html#definition-of-a-style

QML Glow Inside a RowLayout

I am using Qt 5.15 Quick 2 QML to create a row of custom buttons in a window. When I have a standalone custom button things appear to work fine, but when I put them in a RowLayout there appears to be severe clipping and artifacting issues.
A minimum reproducible example might look like:
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Layouts 1.15
import QtQuick.Controls 2.15
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
RowLayout
{
anchors.fill:parent
anchors.margins: 25
Button
{
text: "Click Me"
Layout.fillWidth: true
}
CustomButton
{
text: "That Boy Don't Glow Right"
}
Button
{
x: 100; y:100
text: "Click Me"
Layout.fillWidth: true
}
}
}
with the custom control
import QtQuick 2.0
import QtQuick.Controls 2.15
import QtGraphicalEffects 1.15
Button {
id: control
text: "Click Me"
Glow {
anchors.fill: control
radius: 64
spread: 0
samples: 128
color: "red"
source: control
visible: true
}
}
with example output:
One potential fix is to add change the Glow to
Glow {
anchors.fill: control
width: parent.width
height:parent.height
x:control.x
y:control.y
parent: control.parent
...
But this doesn't seem right. First, it's not obvious to me where parent.width and control.x and control.parent are bound from and what happens in single and multiple nesting. If a CustomButton is placed inside another control with id control, would it rebind the property? And it appears if a RowLayout is placed inside a RowLayout, then it would require parent: control.parent.parent. In my actual code there is some non-trivial positioning to allow margins for a drop shadow, too, and the CustomButton is in another container so the actual code that works is: x:control.x + parent.pad/2 and parent:control.parent.parent.parent which is, frankly, ridiculous and assumes that non-standard fields in the parent are always available.
Is there a better way? Was hoping I could keep the button's ability to glow itself.
According to the docs:
"Note: It is not supported to let the effect include itself, for instance by setting source to the effect's parent."
So it's fortunate that you were able to get your example to work at all. One way to avoid using the parent as a source is to point the Glow object at the Button's background object:
Button {
id: control
Glow {
source: control.background
}
}

QML: Qt.labs.platform.MenuItem's icon

There's a plugin called Qt.labs.platform. Among other things, it provides tray icon with a menu. That menu has a list of MenuItems. Any menu item may have an icon, which would be displayed on the left side of item's text.
There's two ways of setting the icon, none of which work for me:
1) Version 1.0 defined iconSource and iconName properties.
Silently does not work, just shows no icon.
2) Revision 1.1 (declared as Q_REVISION(1)) introduces icon.name, icon.source and icon.mask "sub-properties" (not sure what's the right name for it?)
Fails QML engine with a message:
"MenuItem.icon" is not available in Qt.labs.platform 1.1.
I tried both import Qt.labs.platform 1.1 and 1.0.
Am I missing something in the mechanics of QML revisions or this is a bug in Qt?
A MenuItem is declared in qquickplatformmenuitem_p.h and defined in qquickplatformmenuitem.cpp files.
I'm using ArchLinux, KDE/Plasma. Some other apps (like electron-based) do have their icons in menu showing properly.
UPD Reported as a Qt bug.
I don't know what exactly you are doing to achieve your goal, because there is no minimal reproducible example.
But take a look at here
import QtQuick 2.12
import QtQuick.Window 2.12
// import Qt.labs.platform 1.1
import QtQuick.Controls 2.12
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Rectangle {
width: 200
height: 200
anchors.centerIn: parent
MenuBar {
id: menuBar
Menu {
id: firstMenu
MenuItem {
icon.name: "download"
icon.source: "icons/cloud-down-icon.png"
}
MenuItem {
text: qsTr("Rewrap Paragraph")
onTriggered: rewrapParagraph()
}
}
}
}
}
Note: put your icon inside the icons folder in the project folder.
If you click on the menu then you should get a dropdown with two items, first one is just an icon without the text (you can include some text by yourself), second item is just a text.

How to use Text.ElideMiddle in a Label?

I have a label (shown below) that can display fully qualified file names (including full path).
import QtQml.Models 2.2
import QtQuick.Window 2.2
import QtQuick 2.2
import QtQuick.Controls 1.3
import QtQuick.Controls 2.2 as M2
import QtQuick.Layouts 1.3
ApplicationWindow {
Label {
id: lblSelectedFileId
text: selectedFile
anchors.verticalCenter: parent.verticalCenter
}
}
These file names came become too long to display properly. I would like to accomplish two things:
Learn how to use Text.ElideMiddle to shorten my text.
Learn how to limit the max width of my Label.
I'm sure there is a readily available answer in the documents if I could understand it. Have checked a number of references including these listed below and I still cannot resolve my issues in Python.
Text QML Type | Qt Quick 5.9
https://doc-snapshots.qt.io/qt5-5.9/qml-qtquick-text.html#elide-prop
Elide text in TextField | Qt Forum
https://forum.qt.io/topic/92006/elide-text-in-textfield
“text-overflow” for a QLabel - Stack Overflow
“text-overflow” for a QLabel’s text rendering in QT
Elided Label - Qt Wiki https://wiki.qt.io/Elided_Label
As Label inherits from Text then you can use that property in the same way. On the other hand there is no maximum width but you have to set the width that will be taken into account for ellipsis.
Label{
id: lblSelectedFileId
text: selectedFile
anchors.verticalCenter: parent.verticalCenter
elide: Label.ElideMiddle // or Text.ElideMiddle
width: 50
}

How to use Markdown format in QML 5.14

Recently Qt 5.14 was released. In this version they have added support for the Markdown format.
Added support for the Markdown format (including CommonMark and GitHub dialects) to Text and TextEdit as an alternative to HTML. This includes the GitHub checklist extension, allowing to toggle checkboxes in a TextEdit.
I expect that I can enter text in TextEdit or Text and my text will look like this. Same result you could see in Discord or StackOverflow.
But I have problem with this. I can't find any documentation or any references on how to use it. I have thought, that I can found information in TextEdit textFormat or Text textFormat, but there're only old html tags (they were replaced by Markdown format).
Here's some part of my code, if you need it. (Code can be bugged, because I haven't tested it after changed it.)
import QtQuick 2.14
import QtQuick.Controls 2.14
Item {
width: 100
height: 100
Text {
id: messageText
height: 50
width: 100
text: msgLine.text
wrapMode: Text.WrapAtWordBoundaryOrAnywhere
textFormat: Text.StyledText
font.pointSize: 13
lineHeight: 1.15
anchors.top: parent.top
}
TextEdit {
id: msgLine
height: 50
width: 100
anchors.top: messageText.bottom
Text.RichText // I have changed this value to others
verticalAlignment: Text.AlignVCenter
TextEdit.WrapAtWordBoundaryOrAnywhere
}
}
I want to ask if there is any documentation on how to use it or any example. Thanks in advance!
It seems to be a bug of the Qt documentation(QTBUG-80749), if you want to use markdown in Text or TextEdit then you must set TextEdit.MarkdownText in the textFormat property:
import QtQuick 2.14
import QtQuick.Window 2.14
import QtQuick.Layouts 1.14
Window {
id: root
visible: true
width: 960
height: 480
QtObject{
id: internals
property string markdown_text: "*Italic* **Bold**
# Heading 1
## Heading 2
[Link](http://a.com)
* List
* List
* List
- [x] #mentions, #refs, [links](), **formatting**, and <del>tags</del> supported
- [x] list syntax required (any unordered or ordered list supported)
- [x] this is a complete item
- [ ] this is an incomplete item
First Header | Second Header
------------ | -------------
Content from cell 1 | Content from cell 2
Content in the first column | Content in the second column
"
}
RowLayout{
anchors.fill: parent
TextEdit{
id: te_output
Layout.fillWidth: true
textFormat: TextEdit.MarkdownText
text: internals.markdown_text
}
Text{
id: t_output
Layout.fillWidth: true
textFormat: TextEdit.MarkdownText
text: internals.markdown_text
}
}
}

Resources