qml*_*key 4 qt plasmoid qml qtquick2
我想获取Text元素的实际宽度和高度。如果我使用paintedWidth&paintedHeight属性,则无法理解。例如:
Rectangle {
color: "green"
width: a_text.paintedWidth
height: a_text.paintedHeight
Text {
id: a_text
text: "r"
color: "white"
anchors.centerIn: parent
}
}
Run Code Online (Sandbox Code Playgroud)
如果运行该代码,则"r"在的顶部"r"和下方的左侧会看到一些绿色空间"r"。所以我没有得到真正的宽度和高度Text。我的意思是,白色像素的宽度和高度。
有办法吗?
PS另外,我还需要文本的实际左上角的相对位置,我的意思是,左上白像素相对于的“官方”左上像素的x和y Text。
解决方案是使用新TextMetrics元素(需要QtQuick 2.5)及其tightBoundingRect属性来获取前景文本的实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
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3575 次 |
| 最近记录: |