1

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:

  1. Learn how to use Text.ElideMiddle to shorten my text.

  2. 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.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
MountainX
  • 6,217
  • 8
  • 52
  • 83
  • I recommend you analyze better what tags you use, in this case is it necessary to use the python tag? Not since it is a question relative only to qml. – eyllanesc Jan 12 '20 at 06:11
  • 1
    If you want to learn QML I recommend you read https://qmlbook.github.io/ – eyllanesc Jan 12 '20 at 06:12
  • @eyllanesc I am taking an online course as well as reading. But I am also attempting projects ahead of the course lessons. You seem to be assuming I am not working hard at learning. Not so. I've been at it 12 hours straight today with no break -- because I'm motivated. I worked on the Elided text a solid hour before posting my question. Anyway, your help is greatly appreciated and I want to emphasize that I am not taking it for granted. – MountainX Jan 12 '20 at 06:37
  • @eyllanesc - I am missing something that hasn't been covered in my course or reading yet. Most of the examples I try from the docs don't work. (Your examples do work.) For example, according to this, I should be able to customize the background of a Button: https://doc.qt.io/qt-5/qtquickcontrols2-customize.html However, when I try that example, I get `Invalid property name "background"`. This happens so often that 1) I am sure I am missing a fundamental somethings and 2) learning PyQt5 and QML is more frustrating than other languages I have experience with. – MountainX Jan 12 '20 at 06:57
  • In QML there are many elements that belong to different modules but with the same name, for example [Button](https://doc-snapshots.qt.io/qt5-5.11/qml-qtquick-controls2-button.html) and [Button](https://doc.qt.io/qt-5/qml-qtquick-controls2-button.html) but each one has a different way to customize the items. – eyllanesc Jan 12 '20 at 07:00
  • @eyllanesc and even worse because I am learning Python at the same time. :-) Also, thanks. Now I see that I was using Button from QtQuick.Controls 1.x instead of 2.x. Now I have a background property! – MountainX Jan 12 '20 at 07:06
  • @eyllanesc I wish somebody would have told me that earlier. I initially had the impression that QML would be quick & easy way to build simple GUI's with Python. Clearly I was wrong. – MountainX Jan 12 '20 at 08:05
  • 1
    Actually it is quick to prototype a GUI but another thing is the learning time, for example I use QML to implement the GUI since with the declarative language it is very easy to do it but the first thing is to understand and learn it. – eyllanesc Jan 12 '20 at 08:07

1 Answers1

1

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
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Thanks for your answer. I should have included more code (of what I tried that did not work). I tried this exactly as you show and ElideMiddle has no effect. – MountainX Jan 12 '20 at 06:34
  • Yes, it must be the other part of my code. Your example shows me that I'm not simply making an obvious mistake. It will take me a while to trim it down to a MRE... Thanks again. – MountainX Jan 12 '20 at 06:42