QML: Can a rectangle be drawn from its center and not from its corner?
If I want to render a point in qml, I could do something like this:
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"
}
}
}
However, the rectangle will draw the core in the upper left corner downward. Is there a way to draw a line that is x, y centered?
thank
+3
source to share
1 answer
Not to use Rectangle
as is, but you can create your own elements. Using the transform property allows x
and y
refers to the center of the rectangle.
MyRectangle.qml
:
import QtQuick 2.0
Rectangle {
id: rect
transform: Translate {
x: -rect.width / 2
y: -rect.height / 2
}
}
Then you will use it like this:
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"
}
}
+2
source to share