[Interest] QML Text.Fit question

Curtis Mitch mitch.curtis at theqtcompany.com
Wed Jan 28 13:25:54 CET 2015



> -----Original Message-----
> From: interest-bounces+mitch.curtis=theqtcompany.com at qt-project.org
> [mailto:interest-bounces+mitch.curtis=theqtcompany.com at qt-project.org]
> On Behalf Of Harri Pasanen
> Sent: Wednesday, 28 January 2015 9:32 AM
> To: interest at qt-project.org
> Subject: [Interest] QML Text.Fit question
> 
> If in QML Text item I use Text.Fit size, like
> 
> Text {
>      id: st
>      anchors.centerIn. parent
>      width:  200
>      height: 100
>      fontSizeMode: Text.Fit
>      minimumPointSize: 6
>      font.pointSize: 120
>      text: "Size me if you can!"
> }
> 
> 
> That works ok.   But I did not find a way to query the size it arrived
> at?  If I do console.log(st.font.pointSize) it is still 120 after the
> real size used is obviously not that after display.
> 
> Is there a way to get the size used without dropping to C++?
> 
> Thanks,
> 
> Harri
> 
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest

I can't see a way to access that without using private C++ API.

The actual font size seems to be stored in QQuickTextPrivate::layout. You can see this if you add the following debug statement to qquicktext.cpp:

diff --git a/src/quick/items/qquicktext.cpp b/src/quick/items/qquicktext.cpp
index be86b29..2a7db4e 100644
--- a/src/quick/items/qquicktext.cpp
+++ b/src/quick/items/qquicktext.cpp
@@ -1098,6 +1098,8 @@ QRectF QQuickTextPrivate::setupTextLayout(qreal *const baseline)
     if (truncated != wasTruncated)
         emit q->truncatedChanged();
 
+    qDebug() << layout.font() << font;
+
     return br;
 }

Also, use this modified version of your example:

import QtQuick 2.0

Rectangle {
    width: 200
    height: 200

    Text {
        id: st
        anchors.fill: parent
        fontSizeMode: Text.Fit
        minimumPointSize: 6
        font.pointSize: 120
        text: "Size me if you can!"

        Rectangle {
            anchors.fill: parent
            color: "transparent"
            border.color: "darkorange"
        }
    }
}

As you resize the window, you see something like:

QFont( "Ubuntu,18,-1,5,50,0,0,0,0,0" ) QFont( "Ubuntu,120,-1,5,50,0,0,0,0,0" )
QFont( "Ubuntu,19,-1,5,50,0,0,0,0,0" ) QFont( "Ubuntu,120,-1,5,50,0,0,0,0,0" )
QFont( "Ubuntu,20,-1,5,50,0,0,0,0,0" ) QFont( "Ubuntu,120,-1,5,50,0,0,0,0,0" )

Why do you need to know the size of the text?



More information about the Interest mailing list