Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default margin when using Text QML type

Tags:

text

fonts

qt

qml

Consider this simple code sample:

import QtQuick 2.5
import QtQuick.Window 2.2

Window {
    visible: true
    color: "black"

    Text {
        text: "Hello World!"
        font.family: "Helvetica"
        font.pointSize: 24
        color: "red"
    }
}

enter image description here

Why is there a margin at the top? It seems that giving a custom font with a specific pixelSize gives a new size to the Text and breaks the alignement.

EDIT: well it seems that even without font there still is a margin.

like image 255
Grégoire Borel Avatar asked Jul 10 '26 14:07

Grégoire Borel


1 Answers

A simpler solution would be to set the attribute y of the Text like this:

Text {
    text: "Hello World!"
    font.family: "Helvetica"
    font.pixelSize: 42
    color: "red"
    y: -contentHeight + font.pixelSize
}

Bear in mind that if you set the property font.overline: true then it will be out of the Rectangle

like image 90
lolo Avatar answered Jul 13 '26 15:07

lolo