Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

fontsizemode



  • hi
    I am trying to use fontsizemode because I have to make size of text small based on text length

    my code:

    import QtQuick 2.9
    import QtQuick.Window 2.2
    
    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
        Rectangle
        {
        color: "Red"
        height:50
        width:50
    
        Text { text: "Hello"; fontSizeMode: Text.Fit; minimumPixelSize: 5; font.pixelSize: 50 }
    
    
        }
    
    }
    

    but it is not working is anything wrong in my program?

    0_1548325115344_d9676ffe-4329-4dd6-90b7-5edb0e7ba6b8-image.png



  • Solved this problem
    working code:

    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
        Rectangle
        {
        color: "Red"
        height:50
        width:50
    
        Text {
    width:parent.width
    height: parent.height
     text: "Hello"; fontSizeMode: Text.Fit; minimumPixelSize: 5; font.pixelSize: 50 }
    
    
        }
    
    }
    


  • Hello,

    If you want your text to be fitted in the Rectangle, just add the next line to your Text component:

        anchors.fill: parent
    

    Like this:

        Text {
            anchors.fill: parent
            text: "Hello";
            fontSizeMode: Text.Fit;
            minimumPixelSize: 5;
            font.pixelSize: 50
        }
    

    Now Text will have the same width and height as Rectangle. And using fontSizeMode: Text.Fit will fit both width and height of Rectangle. You can also use Text.HorizontalFit or Text.VerticalFit to fit in width or height.



  • @krokstr Thank you.


Log in to reply