Qt QML:定位到图像的实际显示位置(使用fillMode调整大小)
我想在Qt QML:定位到图像的实际显示位置(使用fillMode调整大小),qt,qml,qt5,Qt,Qml,Qt5,我想在图像的底部添加文本 我不知道图像的大小或其父图像的大小,但图像需要覆盖其父图像的整个空间(整个宽度或整个高度) 我的问题是:由于我在图像上使用了anchors.fill:parent,因此文本位于父组件的底部。如何在不知道其实际位置的情况下将其定位在图像底部 import QtQuick 2.8 import QtQuick.Controls 2.1 ApplicationWindow { visible: true width: 300 height: 500
图像的底部添加文本
我不知道图像的大小或其父图像的大小,但图像需要覆盖其父图像的整个空间(整个宽度或整个高度)
我的问题是:由于我在图像上使用了anchors.fill:parent
,因此文本位于父组件的底部。如何在不知道其实际位置的情况下将其定位在图像底部
import QtQuick 2.8
import QtQuick.Controls 2.1
ApplicationWindow {
visible: true
width: 300
height: 500
Image {
source: "http://images.wikia.com/pt.starwars/images/c/c4/Yoda2.jpg"
fillMode: Image.PreserveAspectFit
anchors.fill: parent // to cover the whole available space
Text {
anchors {
horizontalCenter: parent.horizontalCenter
bottom: parent.bottom
}
text: "Judge me by my size, do you?"
}
}
}
我所拥有的:
我想要的是:
导致该问题的原因是,一个是项目图像的大小,另一个是缩放图像的大小,因为您启用了锚定。填充:父项
和填充模式:图像。保留AspectFit
,第一个是告诉它占据所有屏幕,第二个是以最佳方式缩放,以便观察完整图像
项目图像具有指示缩放图像高度的属性,因此,如果我们在项目图像高度和文本高度旁边使用它,我们将获得所需位置:
import QtQuick 2.8
import QtQuick.Controls 2.1
ApplicationWindow {
visible: true
width: 300
height: 500
Image {
source: "http://images.wikia.com/pt.starwars/images/c/c4/Yoda2.jpg"
fillMode: Image.PreserveAspectFit
anchors.fill: parent // to cover the whole available space
Text {
anchors {
horizontalCenter: parent.horizontalCenter
}
y: (parent.height + parent.paintedHeight)/2 - height
text: "Judge me by my size, do you?"
}
}
}