1
我使用QML顯示背景圖像,並且此圖像需要用圖標疊加。每個這些項目都附有一個MouseArea,因此圖標實際上用作按鈕。QML自定義佈局
現在,圖標需要縮放並隨背景圖像一起移動。它們的佈局行爲需要使圖標看起來好像是背景圖像的一部分(我沒有將圖標包含在背景圖像中,因爲圖標是「活動的」並且與MouseArea相關聯)。
我得到的行爲是圖標可以很好地與背景圖像(當圖像被調整大小時,圖標會相應地重新調整比例)很好地進行比例調整。現在不幸的是,圖標的位置x/y不正確,我不管理找到x/y的公式,以便將圖標與背景調整大小一起移動,並讓圖標成爲背景圖像的一部分。
這裏是我當前的代碼:
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Controls.Styles 1.4
import QtQuick.Controls.Material 2.0
import QtQuick.Layouts 1.3
Rectangle
{
color: palette.grey
// Manual move background and buttons.
Image
{
id: robotBody
fillMode: Image.PreserveAspectFit
anchors.top: parent.top
anchors.topMargin: 10
anchors.horizontalCenter: parent.horizontalCenter
width: parent.width
height: parent.height * 0.8
source: "../UI/Pictures/ManualMove_body.png"
smooth: true
// Buttons.
Image
{
id: xFrontButton
fillMode: Image.PreserveAspectFit
source: "../UI/Icons/ManualMove_XFront.png"
smooth: true
x: 0.8 * parent.paintedWidth
y: 0.8 * parent.paintedHeight
transformOrigin: Item.TopLeft
width: implicitWidth * robotBody.paintedWidth/robotBody.implicitWidth
// NOTE: no need to specify the height because of the preserve aspect ratio fill mode.
MouseArea
{
id: xFrontMouseArea
anchors.fill: parent
hoverEnabled: true
onPressed: { uiController.onXFrontMouseAreaPressed() }
onReleased: { uiController.onXFrontMouseAreaReleased() }
}
}
}
}
什麼錯誤的任何想法?
謝謝,
安託萬。
我說一些我自己的自定義圖像進行測試,命名它們大,小+禁用的東西,那是沒有必要的或不可使用Qt 5.5,我有:做到這一點。 – mlvljr
嗨mlvljr,你知道嗎?你搖滾:它運作良好!現在我不確定我是否理解爲什麼需要這樣做:如果transformOrigin是圖像的中心,我會期望需要*(parent.width - parent.paintedWidth)/ 2 *,但是我實際上已經明確將其更改爲* transformOrigin:Item.TopLeft *,只是爲了避免必須添加此*(parent.width - parent.paintedWidth)/ 2 *。這很好,如果我必須添加這段代碼,這只是我不明白爲什麼我必須... – arennuit
謝謝,原因是,你實際上並沒有改變任何東西(即沒有旋轉或水平),只是通過良好的定位項目舊的x,y,w,h - 然後,所有的x和y值都是從左上角開始的,而w和h則伸展到右側和底部 - 我們沒有其他辦法來處理這個棍子頂部的圖像除了從左側和頂部找到父圖像的可見部分的偏移量(並且當縱橫比保持不變時,給定東西看起來垂直和水平居中 - 我們得到我們所在的位置:))。 – mlvljr