关于 QGraphicsItemGroup 内部项目发生变化后group重新定位的问题

在项目中为了便于对组合后的图元进行管理,一般会继承 QGraphicsItemGroup 实现自己的 group 类,这样可以方便的借用 QGraphicsItemGroup 对内部图元进行管理,但同时也受到了 QGraphicsItemGroup 实现的约束。例如:QGraphicsItemGroup 对象的默认原点坐标为{0,0};对鼠标键盘的消息默认由 QGraphicsItemGroup 处理,内部图元控件不会处理等。这里主要讨论使用 addToGroup() 或 removeFromGroup() 时 group 的 bound 会发生变化,此时需要调整 group 的坐标及尺寸。因为我们可能一次向 group 添加/移除一个控件,也可能添加、移除多个,group 的坐标及尺寸最好在添加/移除后进行调整。但是为了提高代码的内聚性,我们更希望在 group 内部图元发生变化后由 group 自动调整位置及大小。继承 QGraphicsItemGroup 后重载 itemChange() 方法,当内部图元发生变化时可以通过 change == QGraphicsItem::ItemChildAddedChange 监听添加图元的信号、change == QGraphicsItem::ItemChildRemovedChange 监听移除图元的信号。如果在 itemChange() 方法中处理 group 的坐标及尺寸就会发生不可思议的问题:明明位置与尺寸都计算正确,但是内部图元的位置却发生莫名的偏移。通过监控内部图元的坐标发现计算的坐标完全正确,但是显示位置就是不对。如下图,图一是组合前的位置,图二是组合后的位置,组合后显示的选择框就是重新调整后的 group 的位置及大小,内部的矩形与圆形已经偏离了原位置。


图一 组合前的位置


图二 组合后的位置

发生这个问题的原因是:不能在 itemChange() 方法内处理 group 的位置及坐标,因为此时addToGroup() 或 removeFromGroup() 的代码还未执行完毕。看一下 addToGroup() 的源码:

复制代码
// 文件位置 qt-everywhere-src-6.7.3\qtbase\src\widgets\graphicsview\qgraphicsitem.cpp
void QGraphicsItemGroup::addToGroup(QGraphicsItem *item)
{
    Q_D(QGraphicsItemGroup);
    if (!item) {
        qWarning("QGraphicsItemGroup::addToGroup: cannot add null item");
        return;
    }
    if (item == this) {
        qWarning("QGraphicsItemGroup::addToGroup: cannot add a group to itself");
        return;
    }

    // COMBINE
    bool ok;
    QTransform itemTransform = item->itemTransform(this, &ok);

    if (!ok) {
        qWarning("QGraphicsItemGroup::addToGroup: could not find a valid transformation from item to group coordinates");
        return;
    }

    QTransform newItemTransform(itemTransform);
    item->setPos(mapFromItem(item, 0, 0));
	// 设置父项目时会触发 itemChange() 方法
    item->setParentItem(this);

    // removing position from translation component of the new transform
    if (!item->pos().isNull())
        newItemTransform *= QTransform::fromTranslate(-item->x(), -item->y());

    // removing additional transformations properties applied with itemTransform()
    QPointF origin = item->transformOriginPoint();
    QMatrix4x4 m;
    QList<QGraphicsTransform*> transformList = item->transformations();
    for (int i = 0; i < transformList.size(); ++i)
        transformList.at(i)->applyTo(&m);
    newItemTransform *= m.toTransform().inverted();
    newItemTransform.translate(origin.x(), origin.y());
    newItemTransform.rotate(-item->rotation());
    newItemTransform.scale(1/item->scale(), 1/item->scale());
    newItemTransform.translate(-origin.x(), -origin.y());

    // ### Expensive, we could maybe use dirtySceneTransform bit for optimization

    item->setTransform(newItemTransform);
    item->d_func()->setIsMemberOfGroup(true);
    prepareGeometryChange();
    d->itemsBoundingRect |= itemTransform.mapRect(item->boundingRect() | item->childrenBoundingRect());
    update();
}

void QGraphicsItem::setParentItem(QGraphicsItem *newParent)
{
    if (newParent == this) {
        qWarning("QGraphicsItem::setParentItem: cannot assign %p as a parent of itself", this);
        return;
    }
    if (newParent == d_ptr->parent)
        return;

    const QVariant newParentVariant(itemChange(QGraphicsItem::ItemParentChange,
                                               QVariant::fromValue<QGraphicsItem *>(newParent)));
    newParent = qvariant_cast<QGraphicsItem *>(newParentVariant);
    if (newParent == d_ptr->parent)
        return;

    const QVariant thisPointerVariant(QVariant::fromValue<QGraphicsItem *>(this));
	// setParentItemHelper 内部触发 itemChange() 方法
    d_ptr->setParentItemHelper(newParent, &newParentVariant, &thisPointerVariant);
}

void QGraphicsItemPrivate::setParentItemHelper(QGraphicsItem *newParent, const QVariant *newParentVariant, const QVariant *thisPointerVariant)
{
    Q_Q(QGraphicsItem);
    if (newParent == parent)
        return;
	...
	if (parent) {
        // Remove from current parent
        parent->d_ptr->removeChild(q);
        if (thisPointerVariant)
            parent->itemChange(QGraphicsItem::ItemChildRemovedChange, thisPointerVariant);
    }
	...
	 // Deliver post-change notification
    if (newParentVariant)
        q->itemChange(QGraphicsItem::ItemParentHasChanged, *newParentVariant);

    if (isObject)
        emit static_cast<QGraphicsObject *>(q)->parentChanged();
}

通过源码可以发现,如果在 itemChange() 内部处理 group 的坐标及尺寸,确实会出现很多问题,因为此时 addToGroup() 还未执行 transform 变换。
要解决此问题,就必须等待addToGroup() 执行完成再去计算坐标及尺寸。可以在 itemChange() 发射一个信号,采用异步处理该信号,将处理过程推迟到下一个事件循环。这样就能够完美解决问题。

具体代码可以参考项目 Compelling Data Designer 中 dashboard/BIDesigner/bigraphicsview.cpp 的处理过程。该项目用于数据的可视化设计,软件采用可扩展架构,支持扩展图形插件、数据接口。项目仍在开发中,目前已设计完成基本图形、多属性配置、动画等功能。

相关推荐
带土11 小时前
5. QT之Q_OBJECT详解
开发语言·qt
IT永勇1 小时前
C++设计模式-装饰器模式
c++·设计模式·装饰器模式
Murphy_lx2 小时前
std_ofstream
c++
草莓熊Lotso2 小时前
红黑树从入门到进阶:4 条规则如何筑牢 O (logN) 效率根基?
服务器·开发语言·c++·人工智能·经验分享·笔记·后端
啊董dong2 小时前
课后作业-2025年11月23号作业
数据结构·c++·算法·深度优先·noi
带鱼吃猫2 小时前
Linux系统:策略模式实现自定义日志功能
linux·c++
zzzsde2 小时前
【C++】C++11(1):右值引用和移动语义
开发语言·c++·算法
学困昇2 小时前
C++11中的包装器
开发语言·数据结构·c++·c++11
雪域迷影3 小时前
C++中编写UT单元测试用例时如何mock非虚函数?
开发语言·c++·测试用例·gmock·cpp-stub开源项目