OverviewFelgo 1 Support › Problems manually positioning rectangles within each other

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
  • #7699




    I am trying to manually position a rectangle within another rectangle. I have the following code:


    Rectangle {
            id: area
            x: parent.width - area.width / 2
            height: 30
            color: "red"
            width: 400
            Rectangle {
                id: hitArea
                x: parent.x 
                y: parent.y
                height: 30
                width: 30
                color: "blue"

    The problem is the rectangle hitArea’s x-co-ordinate is actually calculated as being parent.width – (area.width / 2) but is instead calculated as being parent.width. To make this work I need to set the hitArea‘s x property to be parent.x + area.width / 2.

    While this works it doesn’t look very elegant. Why is the hitArea not taking its x property as I expect it would (i.e after the parent’s x property has been evaluated)?

    BTW I don’t want to use anchors here because ultimately I want to be able to position the hit area randomly along width of the area according to some parameters.


    Felgo Team

    Hi Stan,

    x and y values are always relative to the container. If your “area” is in a scene and has the position x:50 and y:50, your code will cause the “hitArea” to have x:50 and y:50 relative to the “area”. So it will be x:100 and y:100 in total.

    There are 2 solutions:

    – Do not set the x and y coordinates of the “hitArea” (or set them to 0), because since it is a child, it will move with the parent if the parent position is changed.

    – Do not make the “hitArea” a child of the “area”, but instead put it next to it and then your parent.x and parent.y will work as expected.




    Thanks Alex. That has cleared it up for me.


    Felgo Team

    I’m glad i could help 🙂


Viewing 4 posts - 1 through 4 (of 4 total)

RSS feed for this thread

You must be logged in to reply to this topic.

Voted #1 for:

  • Easiest to learn
  • Most time saving
  • Best support

Develop Cross-Platform Apps and Games 50% Faster!

  • Voted the best supported, most time-saving and easiest to learn cross-platform development tool
  • Based on the Qt framework, with native performance and appearance on all platforms including iOS and Android
  • Offers a variety of plugins to monetize, analyze and engage users
create apps
create games
cross platform
native performance
3rd party services
game network
level editor
easiest to learn
biggest time saving
best support