QML:我可以从它的中心而不是从它的角画一个矩形吗?

QML:我可以从它的中心而不是从它的角画一个矩形吗?,qml,Qml,如果我想在qml中可视化一个点,我可以这样做: import QtQuick 2.4 import QtQuick.Window 2.2 import QtQuick.Controls 1.2 import QtQuick.Layouts 1.0 ApplicationWindow{ width: 1024 height: 256 visible: true ColumnLayout { anchors.fill: parent

如果我想在qml中可视化一个点,我可以这样做:

import QtQuick 2.4
import QtQuick.Window 2.2
import QtQuick.Controls 1.2
import QtQuick.Layouts 1.0

ApplicationWindow{
    width: 1024
    height: 256
    visible: true

    ColumnLayout {
        anchors.fill: parent

        Rectangle {
            x: 10
            y: 10
            width: 5
            height: 5
            color: "white"
        }
    }
}
然而,矩形将把drom的左上角画下来。有没有办法画出以x,y为中心的矩形


感谢您不要按原样使用矩形,但您可以自己制作物品。使用该属性允许
x
y
引用矩形的中心

MyRectangle.qml

import QtQuick 2.0

Rectangle {
    id: rect

    transform: Translate {
        x: -rect.width / 2
        y: -rect.height / 2
    }
}
然后您可以这样使用它:

import QtQuick 2.0
import QtQuick.Window 2.0

Window {
    width: 200
    height: 200
    visible: true

    MyRectangle {
        x: 50
        y: 50
        width: 50
        height: 50
        color: "darkorange"
    }
}