PDA

View Full Version : How to elide text in TextEdit



Qyrychen
3rd February 2016, 12:22
hi All!
There is Qml model and TableView in my application.
TextEdit is used in delegate for text displaying (I use this element because I will use QSyntaxHighlighter with it)
Also I use wrapMode: Text.NoWrap

After column width changing I want get elide string (like ThisIsATex... for example)
I use TextMetrics for text eliding (http://doc.qt.io/qt-5/qml-qtquick-textmetrics.html#width-prop)


onWidthChanged: {
textMetrics.text = delegateTextEdit.text
textMetrics.elideWidth = delegateTextEdit.width - 10
var str = textMetrics.elidedText
console.debug(str)

//delegateTextEdit.text = str
}

Problem:
- on width changing I can see correct elide text in console (in TableView it doesn't appear)
11675
- after last line uncommented I see text only at first column in my table. Yes, text is elided but only in one way
11676

anda_skoa
3rd February 2016, 13:28
You will need a property that holds the actual text and use that as the input for your elide calculation.
You can probably use the elidedText as the input for the TextEdit though.

Something like



TextEdit {
id: delegateTextEdit

property string fullText: model.text
text: deletateTextEditMetrics.elidedText

TextMetrics {
id: delegateTextEditMetrics

text: delegateTextEdit.fullText
elideWidth: delegateTextEdit.width - 10
}
}


Cheers,
_

Qyrychen
4th February 2016, 15:55
Fixed/ My code

TextMetrics {
id: textMetrics
elide: Text.ElideRight

function getElidedText(originalText, maxWidth){
if(originalText != undefined){
text = originalText
elideWidth = maxWidth
return elidedText
}
}
}

TextEdit{
//...
wrapMode: Text.NoWrap

onWidthChanged: {
text = textMetrics.getElidedText(styleData.value, width)
}

onVisibleChanged: {
text = textMetrics.getElidedText(styleData.value, width)
}

anda_skoa
4th February 2016, 16:38
Did the declarative approach not work?

Cheers,
_

joko
8th November 2016, 10:19
Hi @anda_skoa i did what you suggested above however, i seems have a problem with android.
I found out that the elideWidth (TextInput.width - 10) is not enough to elide the text, when I tried deducting 200 from the width, it works fine.
Btw, the provided example works perfectly in iOS.

posted here (https://forum.qt.io/topic/73093/textmetrics-elidewidth-is-not-working-propertly-on-android) is the sample code.

Any advise? Thanks.

anda_skoa
8th November 2016, 17:42
Maybe 10 is not enough offset, e.g. there could be margin or padding that is platform style specific, etc.

Cheers,
_

joko
9th November 2016, 09:54
Maybe 10 is not enough offset, e.g. there could be margin or padding that is platform style specific, etc.

Cheers,
_

Thanks for the reply.

I was able to resolved the issue by explicitly setting the font.pointSize in Android.