QML Text : How to know currently displaying characters ?



  • When text truncated in Text element how to get the exact displayed Text from text Element. For example from the follwoing code ...

                Text {
                    id: txt
                    text: qsTr("ABCDEFGHIJKLMNOP")
                    maximumLineCount: 1
                    elide:Text.ElideRight
                    font.pointSize: 30
                    anchors.fill: parent
                    anchors.margins: 10
                    verticalAlignment: Text.AlignVCenter
                }
    

    Consider Text width is 300 , and the text truncated like "ABCDEFGH...." How can i get the string which is getting displayed out of complete string. I know truncated property will be true and will only tells text is truncated. But here i need Text which is displayed.



  • @DonCoder Try FontMetrics.elidedText(). You would have to call it in onTruncatedChanged, I think.



  • @Eeli-K : Correct, This would really help. Thanks
    a lot

    TextMetrics  {
                    id: t_metrics
                    text: qsTr("ABCDEFGHIJKLMNOP")
                    elide:Text.ElideRight
                    elideWidth: 250
                    font.pointSize: 30
                }
    
                Text {
                    id:txt
                    x: 316
                    y: 275
                    maximumLineCount: 1
                    anchors.margins: 10
                    anchors.fill: parent
                    verticalAlignment: Text.AlignVCenter
                    text:   t_metrics.elidedText
                    font.pointSize: 30
                }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.