[Qt-qml] Text eliding

Bo Thorsen bo at fioniasoftware.dk
Wed Sep 7 07:39:27 CEST 2011


No, I can't get the width of the text item, because the two strings 
should be centered.

Bo.

Den 06-09-2011 14:44, Mark Tucker skrev:
> Hello,
>
> The Text item required a width to be specified (either explicitly via
> the width property or implicitly with anchors) so that it knows what
> area the Text is supposed to be fitting inside, so that it can then
> perform the elide.
>
> With the code below there is no width set. It then doesn't know where to
> elide the text because it doesn't know what width the text is supposed
> to be fitting inside.
>
> You should easily be able to specify its width based on its parent
> Item's width.
>
> Mark T
>
> -----Original Message-----
> From: qt-qml-bounces+mark.tucker=airborne.aero at qt.nokia.com
> [mailto:qt-qml-bounces+mark.tucker=airborne.aero at qt.nokia.com] On Behalf
> Of Bo Thorsen
> Sent: 06 September 2011 09:08
> To: qt-qml at qt.nokia.com
> Subject: [Qt-qml] Text eliding
>
> I'm trying to ellide a text item, but it's proving quite complicated.
> I'm tempted to implement my own C++ based QML class that does this, but
> it just seems silly.
>
> The problem is that Text says it can only elide when you have set the
> width, but I don't have that.
>
> I have something like this:
>
> Item {
>     width: parent.width
>     anchors.centerIn: parent
>     Text {
>       id: text1
>       text: ...
>       elide: Text.ElideRight
>       horizontalAlignment: Text.AlignHCenter
>       verticalAlignment: Text.AlignVCenter
>       font.bold: true
>     }
>     Text {
>       id: text2
>       text: ....
>     }
> }
>
> (most anchors and stuff like that has been omitted)
>
> So I want text1 to elide on the right side, when the combined text of
> the two doesn't fit in the row. What I actually have here is a max width
>
> which is parent.width - text2.width. In HTML, it would look like this:
> "<center><bold>text1</bold>text2</center>", except that text1 should be
> elided.
>
> It might be that there is a way to set something like this on the text2
> width:
>
> width: max(font.textWidth(text), parent.width - text2.width)
>
> But I haven't been able to figure out how to do that.
>
> Any good ideas?
>
> Bo Thorsen,
> Fionia Software.
>


Bo Thorsen,
Fionia Software.

-- 

Expert Qt and C++ developer for hire
Contact me if you need expert Qt help
http://www.fioniasoftware.dk


More information about the Qt-qml mailing list