PDA

View Full Version : help with ZValue!!



rachana
30th January 2007, 11:01
Hi all,

I have mainwindow and it has multiple objects of QGraphicsItem and I want to use a global variable (for Z). Now when the mouse click is over a QGraphicsItem I will increment the Z value so that current QGraphicsItem will be on the top even though there are other
overlapping items. Can somebody suggest what is the best way to access the global Z value and increment it?

Rachana

aamer4yu
30th January 2007, 12:19
One way is to maintain global highest Zvalue. When a item is clicked, set its zvalue as highest zvalue. And when the item is released, set the original zvalue of the item.


Second approach is to have zvalue, and highest zvalue as member variables of the graphics item. In this way u can set the zvalues at your ease. It will also help if you have different types of items and u want a particular type of item should always appear below other item.
Hope this helps

munna
30th January 2007, 12:26
May be you can make it static.

Bitto
31st January 2007, 20:47
A simple local approach is to find a Z value by checking all colliding items. If you use Qt::IntersectsItemBoundingRect, this operations is very fast.



// Find largest Z
qreal maxZ = 0;
foreach (QGraphicsItem *item, clickedItem->collidingItems(Qt::IntersectsItemBoundingRect))
maxZ = qMax(maxZ, item->zValue());

// Assign new Z
clickedItem->setZValue(maxZ + some);


If you nest items (child items etc), you'll have to compare against top level items' Z values:



// Find largest Z
qreal maxZ = 0;
foreach (QGraphicsItem *item, clickedItem->collidingItems(Qt::IntersectsItemBoundingRect))
maxZ = qMax(maxZ, item->topLevelItem()->zValue());

// Assign new Z
clickedItem->topLevelItem()->setZValue(maxZ + some);


Another approach is to normalize the Z values of all colliding items, for example like this:



int z = 0;
foreach (QGraphicsItem *item, clickedItem->collidingItems(Qt::IntersectsItemBoundingRect))
item->topLevelItem()->setZValue(--z);
clickedItem->topLevelItem()->setZValue(0);


Hope that helps. :-)