QGraphicsItem插入顺序
我有一个使用 QGraphicsScene + QGraphicsView 进行 QGraphicsItem 组织的项目。 所有图形项目都是可移动和可选择的。
问题是:当我在场景中插入新的 QGraphicsItem 并且该项目比前一个项目大时,我无法通过鼠标选择它。
来自 Qt 文档:
默认情况下,所有同级项目均按插入顺序堆叠(即,您添加的第一个项目在您添加的下一个项目之前绘制)。如果两个项目的 Z 值不同,则具有最高 Z 值的项目将绘制在顶部。当Z值相同时,插入>顺序将决定堆叠顺序。
问题,如何更改图形项的插入顺序?
我尝试为 QGraphicsView 子类化 mousePressEvent 并使用 stackBefore() 方法帮助更改插入顺序。 如果所有项目混合在一起并相互重叠,则需要进行多次单击才能选择所需的项目。
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 );
}
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 );
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
刚刚在文档中找到:
希望有帮助。
just found in docu:
Hope it helps.