3
votes

I have project which use QGraphicsScene + QGraphicsView for QGraphicsItem organizing. All graphics items are movable and selectable.

The problem is: when I insert new QGraphicsItem on the scene and this item bigger then previous item, I can't select it by mouse.

From Qt documentation:

By default, all sibling items are stacked by insertion order (i.e., the first item you add >is drawn before the next item you add). If two items' Z values are different, then the item >with the highest Z value is drawn on top. When the Z values are the same, the insertion >order will decide the stacking order.

Question, How to change inserting order for graphics items ?

I try to subclass mousePressEvent for QGraphicsView and change inserting order with stackBefore() method help. If all items mixed together and overlap each other need to do a lot of clicks to select needed item.

void View::mousePressEvent ( QMouseEvent * event )
{

MyGraphicsGroup *last = 0;
QGraphicsItem *first = 0;
foreach( QGraphicsItem *item, items(event->pos()) )
{
    if( !first ) first = item;

    if( item->flags() & QGraphicsItem::ItemIsSelectable )
    {
        last = qgraphicsitem_cast<MyGraphicsGroup*>(item);
    }
}

if( last )
{
    first->stackBefore(last);
}

inherited::mousePressEvent( event );
}
1

1 Answers

2
votes

just found in docu:

"You can set an item's stacking order by calling setZValue()"

Hope it helps.