I have a project that uses QGraphicsScene + QGraphicsView to organize QGraphicsItem. All graphic elements are movable and selectable.
The problem is that when I insert a new QGraphicsItem object into the scene, and this item is larger than the previous item, I cannot select it with the mouse.
From the Qt documentation:
By default, all sibling elements are added in insertion order (i.e. the first element you add> is drawn before the next element is added). If the values of the two Z elements are different, then the> element with the highest Z value is drawn on top. When the Z values are the same, the insertion order> will determine the stacking order.
Question: How to change the insertion order for graphic elements?
I am trying to subclass mousePressEvent for a QGraphicsView and change the insertion order using the stackBefore () method method. If all elements mixed with each other and overlapping each other have to make many clicks to select the desired element.
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 );
}
source
share