我可以得到文本元素的真实宽度和高度吗?



我想得到实际宽度&CCD_ 1元素的高度。如果我使用CCD_ 2&paintedHeight属性,我不明白。例如:

Rectangle {
    color:  "green"
    width:  a_text.paintedWidth
    height: a_text.paintedHeight
    Text {
        id:     a_text
        text:   "r"
        color:  "white"
        anchors.centerIn: parent
    }
}

如果我运行该代码,我会在"r"顶部和"r"下方的"r"左侧看到一些绿色空间。所以我没有得到真正的宽度&CCD_ 7的高度。我的意思是,宽度&白色像素的高度。

有什么办法吗?

附言:我还需要文本左上角的相对位置,我的意思是,x&y相对于CCD_ 8的"官方"左上像素。

解决方案是使用新的TextMetrics元素(需要QtQuick 2.5)及其Text0属性,以获得真正的width&前台文本的heigth

import QtQuick 2.5      // required!
Rectangle {
    color:  "green"
    width:  t_metrics.tightBoundingRect.width
    height: t_metrics.tightBoundingRect.height
    Text {
        id:     a_text
        text:   "r"
        color:  "white"
        anchors.centerIn: parent
    }
    TextMetrics {
        id:     t_metrics
        font:   a_text.font
        text:   a_text.text
    }
}

"your_text_object.paintedHeight"或";your_text_object.paintedWidth":是您需要获得文本的高度,包括超过所覆盖高度的高度,因为文本的数量超过了设置的高度。

绘制的文本QML元素高度

最新更新