PDA

View Full Version : Sorting objects with setZValue()



IK
14th May 2012, 15:12
Hi
I'm new to Qt (using Qt Creator 2.1.0 with Qt version 4.7.2) and have an issue with sorting objects in to the correct order. i.e. I want to understand how to correctly use setZValue() in order to do that.

My app allows for insertion of three types of objects in to a work area.
e.g.
object 1: triangle
object 2: rectangle
object 3: circle

What I want to happen is that whenever a new circle is inserted it should always go on top of all other objects (including existing circles) that are already in the work area.
I give Z-values of 1,2 and 3 to triangle, rectangle and circle respectively.
e.g.


MyAppCircle *item = new MyAppCircle(this);
item->setZValue(3)

Results of test run:
1) add rectangle. OK
2) add circle. it goes on top of rectangle. OK
3) click on visible part of rectangle. rectangle comes in to foreground and circle moves back. OK
4) add a new circle. it goes in the middle of the two existing object. i.e. behind rectangle and in front of the old circle. Not OK. As with a Z-value of 3 I expect it to go to the top of the stack.

I hope I've managed to explain the issue clearly.
Any help would be much appreciated.

norobro
14th May 2012, 20:59
Aren't you changing the zValue of the rectangle to greater than 3 to bring it to the top of the stack? If that is the case, the added circle would be on top of the first circle but below the rectangle.