Mysterious height handling and Point size <= 0 assert
-
Our QML application has a lot of text elements and all is looking good. But during the initialization we get a lot of point size errors for a number of Text elements.
@QFont::setPointSizeF: Point size <= 0 (0.000000), must be greater than 0@
The reason for this assert is that during the initialization phase the height of the element is empty (=0). I have created a small example to demonstrate this:
@import QtQuick 2.2
Rectangle {
id: main
width: 640
height: 480onHeightChanged: {
console.log("new height: ", height)
}Text {
id: myText
height: parent.height
text: "Font Size Text"
font {
family: "Arial"
pointSize: 0.10 * myText.height
}onFontChanged: {
console.log("new pointSize : ", myText.font.pointSize)
console.log(" myText height: ", myText.height)
console.log(" main height: ", main.height)
console.log(" parent height: ", parent.height)
}horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
anchors.centerIn: parent
}
}@The result is the following output:
@qml: new pointSize : 8.25
qml: myText height: 0
qml: main height: 480
qrc:/qml/helloQml/FontSize.qml:26: TypeError: Cannot read property of null
qml: new pointSize : 48
qml: myText height: 0
qml: main height: 480
qml: parent height: 480@I have also used the QML Profiler to analyze the timeline and the pointSize is the first property that is binded and computed. So the resulting behavior is clear.
But what is the best way to prevent the assert message?
Thanks
-
@onFontChanged: {
console.log("new pointSize : ", myText.font.pointSize)
console.log(" myText height: ", myText.height)
console.log(" main height: ", main.height)
console.log(" height: ", height)
}@onFontChanged refers to the Rectangle with "id: main". You don't have to call to the parent.height property.
Edited: I've read again and I don't answer your question, I'll look at it.
Edited2: Try this.
@ font {
family: "Arial"
}Component.onCompleted: myText.font.pointSize = 0.10 * myText.height@
-
thanks, but this will not solve the problem:
- we lose the dynamic height if somewhere changes the parent geometry
- the assert message is also shown because the binding of parent.height is in the timeline after the signal handling (using QML Profiler)
At the moment my solution is
@font {
family: "Arial"
pointSize: getFontSize(0.10 * height)
}@where getFontSize is a very simple function
@function getFontSize(s)
{
return s === 0 ? 1 : s;
}@But over all I haven't found some useful documentation how can we handle all the sizes of a QML application. From my point of view I need to lot width, height informations also when I use layouts.