Results 1 to 2 of 2

Thread: QGraphicsItem zValue question

  1. #1
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default QGraphicsItem zValue question

    I need some help understanding how QGraphicsItem::zValue() works in relationship to clipping. I have a scene with many text labels (QGraphicsSimpleTextItem-based). Because some sections of the scene can have many labels, it is possible for the labels to overlap. I am using QGraphicsItem::setZValue() to put a z-ordering on the labels, with more "important" labels having higher z values. All of the labels are siblings and children of a single parent QGraphicsItem. The labels can't be opaque; the scene information "behind" the labels has to show through.

    I am having several problems:

    - all of the items are painted, no matter what their z-value, so the result is a mess of overlapped labels.
    - if I set QGraphicsItem::ItemClipsToShape for the labels, none of them are drawn, even ones that don't overlap
    - if I call QGraphicsItem::collidingItems( Qt::IntersectsItemBoundingRect ) on any one of them, I get a list that contains only the parent or other non-label items in the scene.

    If I try with this code ("textItems" is a std::vector<> that contains all of the siblings with the QGraphicsSimpleTextItem::Type ):

    Qt Code:
    1. std::vector< QGraphicsItem * >::iterator it = textItems.begin();
    2. std::vector< QGraphicsItem * >::iterator eIt = textItems.end();
    3. while ( it != eIt )
    4. {
    5. QGraphicsItem * pItem = *it++;
    6. if ( pItem->isVisible() )
    7. {
    8. std::vector< QGraphicsItem * >::iterator oIt = it;
    9. while ( oIt != eIt )
    10. {
    11. QGraphicsItem * pOther = *oIt++;
    12. if ( pItem->collidesWithItem( pOther, Qt::IntersectsItemBoundingRect ) )
    13. {
    14. if ( pItem->zValue() < pOther->zValue() )
    15. {
    16. pItem->setVisible( false );
    17. break;
    18. }
    19. else
    20. pOther->setVisible( false );
    21. }
    22. }
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 

    then the collidesWithItem does return true for many labels, but I still end up with overlaps in some places anyway, and this code is very, very slow since there are thousands of labels. It gets executed with every scene transformation (for example, zooming or panning).

    Is there some way of getting QGraphicsScene / QGraphicsView to do this clipping automatically?

  2. #2
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsItem zValue question

    I think z-value has nothing to do with clipping, it's just an order in which items are drawn on the screen.

    Also I'm not sure what you're trying to do.
    You're talking about clipping but you hide items with lower z-value.

    Do you want part of the labels 'below' to be visible when obscured by other label?
    Or do you want to hide labels that are even partially obscured by other labels?

    To be able to help you, you need to share little more of your requirements.
    Is the labels position static or dynamic?
    Is the labels geometry static or dynamic (changing label content changes label geometry)?
    What should happen if two labels of the same z-order overlap?

    I would also question why all labels (thousands?) have the same parent item?
    Can't you break them into smaller groups and investigate which one should be visible when one item in the group changes.
    Also, because when zooming or panning, items themself do not change their geometry, none of the calculations you did to figure out what to show should be done again.

Similar Threads

  1. About QGraphicsItem question
    By jiaorenjie in forum Qt Programming
    Replies: 1
    Last Post: 16th March 2011, 08:38
  2. QGraphicsItem pos() method question!
    By zgulser in forum Qt Programming
    Replies: 0
    Last Post: 20th August 2009, 19:56
  3. Simple QGraphicsItem question
    By godmodder in forum Qt Programming
    Replies: 2
    Last Post: 13th November 2008, 17:34
  4. help with ZValue!!
    By rachana in forum Qt Programming
    Replies: 3
    Last Post: 31st January 2007, 19:47
  5. QGraphicsItem move question
    By mhoover in forum Qt Programming
    Replies: 2
    Last Post: 7th December 2006, 19:01

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.