How to use Text.ElideMiddle in a Label? - qt

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
}

Related

Unable to use ttf icon font in QML

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.

QML Button exclusiveGroup property?

I'm unable to assign ExclusiveGroup to my checkable buttons.
ExclusiveGroup {
id: group
onCurrentChanged: {
if(current != null) {
console.info("button checked...no!")
current = null
//current.checked = false <--- also this
}
}
}
Column {
width: parent.width
spacing: 0
Button {
id: btnScan
flat: true
text: qsTr("But1")
width: parent.width
checkable: true
exclusiveGroup: group
}
Button {
id: btnWhiteList
flat: true
text: qsTr("But2")
width: parent.width
checkable: true
exclusiveGroup: group
}
}
The documentation states clearly that Button does have exclusiveGroup property http://doc.qt.io/qt-5/qml-qtquick-controls-button.html#exclusiveGroup-prop. However, when I run the example, I get this error:
qrc:/main.qml:48 Cannot assign to non-existent property "exclusiveGroup"
Hovering mouse over "exclusiveGroup" makes a tooltip show up saying: "Invalid property name exclusiveGroup".
I have Qt 5.9.1 installed. Here's my import statements:
import QtQuick 2.7
import QtQuick.Controls 1.4
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3
import QtQuick.Controls.Material 2.2
What am I doing wrong?
Thanks
The reason for your problem is this:
import QtQuick.Controls 1.4
import QtQuick.Controls 2.0
You have a Button in both, but they have a different API.
So at first you import the Button from QtQuick.Controls 1.4. Then you import the Button from QtQuick.Controls 2.0. As QML doesn't know, which one you want to use, it will take the one, you imported the last. If you want to be more specific, on which Button you want to use, you can use named imports
import QtQuick.Controls 1.4 as OldCtrl
import QtQuick.Controls 2.0 as NewCtrl
Then you can use Buttons from both versions as you like:
OldCtrl.Button { // from the QtQuick.Controls 1.4 }
NewCtrl.Button { // from the QtQuick.Controls 2.0 }
The documentation you quote is for QtQuick.Controls 1.x, and from there is the imported ExclusiveGroup. So you are trying to mix things from two libraries, that wont work together.
See ButtonGroup for a similar solution for QtQuick.Controls 2.x
For more on the differences and usecases of the both sets of controls read:
http://blog.qt.io/blog/2016/06/10/qt-quick-controls-2-0-a-new-beginning/
https://doc.qt.io/qt-5/qtquickcontrols2-differences.html

Cannot find Qt QML SpinBox minimumValue using QtQuick.Controls 2.1

I got this piece of code:
import QtQuick 2.7
import QtQuick.Controls 1.5
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("spinbox trouble")
SpinBox{
anchors.centerIn: parent
value: 5
minimumValue: 1
maximumValue: 10
}
}
Everything is fine. Now I want to use SwipeView. To use it I need use QtQuick.Controls 2.1. But when I use version 2.1, I cannot find [minimum/maximum]Value in SpinBox.
Questions:
1. What is reason to drop this properties in QtQuick.Controls 2.1?
2. Is there any way to use both SwipeView and SpinBox with [minimum/maximum]Value property?
Looking at the Qt doc I'd say you can use the from and to properties instead of minimumValue and maximumValue!?

How to use a GridLayout in combination with exposing data property

I am trying to wrap GridLayout inside an Item and exposing the GridLayout's data property as the default property of the item. But this results in two problems.
I get a crash when exiting the application.
This may in fact be a bug in Qt itself and it might also already been fixed, if not I will report it after fixing 2. I am only able to test on Windows 7 using Qt 5.7.0 MSVC2015 32.bit at the moment.
How to use the attached Layout property? Take look in the following example, which results in the error:
Non-existent attached object
on line
"Layout.alignment: Qt.AlignBottom | Qt.AlignRight".
Example:
//MyCustomLayout.qml
import QtQuick 2.7
import QtQuick.Layouts 1.3
Item {
default property alias data: layout.data
//Some other QML components not to be within GridView here.
GridLayout {
id: layout
anchors.fill: parent
}
//Some other QML components not to be within GridView here.
}
//main.qml
import QtQuick.Controls 2.0
ApplicationWindow {
id: root
visible: true
height: 1024
width: 768
MyCustomLayout {
anchors.fill: parent
Button {
Layout.alignment: Qt.AlignBottom | Qt.AlignRight
}
}
}

Material theme doesn't seem to work in QML

I have a simple QML with an ApplicationWindow, RowLayout and a bunch of Buttons inside. I have applied the Qt Quick Controls 2 Material theme as per the docs, but nothing changed. What's wrong?
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Controls.Material 2.0
import QtQuick.Layouts 1.3
ApplicationWindow {
Material.theme: Material.Dark
Material.accent: Material.Orange
id: window
visible: true
RowLayout {
anchors.horizontalCenter: window.horizontalCenter
anchors.bottomMargin: 32
Button {
text: "A"
}
Button {
text: "B"
}
Button {
text: "C"
}
}
}
Importing QtQuick.Controls.Material 2.0 and setting some Material specific properties do not apply the Material theme. They will be used if the theme is set using one of the methods described here:
http://doc.qt.io/qt-5/qtquickcontrols2-styles.html

Resources