QGraphicsItem插入顺序

发布于 2024-11-10 17:29:30 字数 903 浏览 4 评论 0原文

我有一个使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

马蹄踏│碎落叶 2024-11-17 17:29:30

刚刚在文档中找到:

“您可以设置项目的堆叠顺序
通过调用 setZValue()"

希望有帮助。

just found in docu:

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

Hope it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文