Search code examples
qtdrawingcoordinatesqmlqt-quick

Drawing to the left of x,y coordinates


I'm working currently on Qt, when i try to draw something (for e.x Rectangle) giving its x,y coordinates & its width & height Qt starts to draw the rectangle starting from its x, y coordinates to the right direction, to demonstrate what I'm talking about see the following picture:

Rect1

and the following code:

Rectangle {
    id: rectangle1
    x: 257
    y: 221
    width: 50
    height: 50
    color: "#000000"
}

I would like to know if there is anyway i can start drawing to the left of x,y coordinates, it's like drawing giving the width & height negative values. see the following image for illustration:

Rect2

I have tried drawing giving the width a negative value, it works fine on the design mode but when i run or debug my application the rectangle does not show up, any ideas on this would be appreciated


Solution

  • You can not use negative values for this purpose since negative width or height is not supported in QML. But you can use Scale type to accomplish this :

    Rectangle {
        id: rectangle1
        x: 257
        y: 221
        width: 50
        height: 50
        color: "#000000"
    
        transform: Scale { origin.x: 0; origin.y: 0; xScale: -1}
    }
    

    Scale allows you to scale your item relative to an arbitrary point. Here we scaled the X axis of the item relative to it's interior point (0, 0).