开发者

QGraphicsItem inserting order

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 ov开发者_开发知识库erlap 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 );
}


just found in docu:

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

Hope it helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜