Results 1 to 4 of 4

Thread: help with ZValue!!

  1. #1
    Join Date
    Jan 2007
    Posts
    13
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default help with ZValue!!

    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

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: help with ZValue!!

    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

  3. #3
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: help with ZValue!!

    May be you can make it static.

  4. #4
    Join Date
    Jan 2006
    Location
    Norway
    Posts
    124
    Thanked 38 Times in 30 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: help with ZValue!!

    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.

    Qt Code:
    1. // Find largest Z
    2. qreal maxZ = 0;
    3. foreach (QGraphicsItem *item, clickedItem->collidingItems(Qt::IntersectsItemBoundingRect))
    4. maxZ = qMax(maxZ, item->zValue());
    5.  
    6. // Assign new Z
    7. clickedItem->setZValue(maxZ + some);
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. // Find largest Z
    2. qreal maxZ = 0;
    3. foreach (QGraphicsItem *item, clickedItem->collidingItems(Qt::IntersectsItemBoundingRect))
    4. maxZ = qMax(maxZ, item->topLevelItem()->zValue());
    5.  
    6. // Assign new Z
    7. clickedItem->topLevelItem()->setZValue(maxZ + some);
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. int z = 0;
    2. foreach (QGraphicsItem *item, clickedItem->collidingItems(Qt::IntersectsItemBoundingRect))
    3. item->topLevelItem()->setZValue(--z);
    4. clickedItem->topLevelItem()->setZValue(0);
    To copy to clipboard, switch view to plain text mode 

    Hope that helps. :-)
    Bitto / Andreas Aardal Hanssen - andreas dot aardal dot hanssen at nokia
    Nokia Software Manager, Qt Development

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.