Domanda

I have many rectItem in a

QVector <GraphicsRectItem *> list_rect_item;

I group them in a QGraphicsItemGroup to make them rotate from the middle of my scene :

for(int i=0 ; i < list_rect_item.size() ; i++) {
    group->addToGroup(list_rect_item.at(i));
}

QPoint point_center(scene.width()/2, scene.height()/2);
group->setTransformOriginPoint(point_center);
group->setRotation(ui->box_angle->value());

But now I want to know there new coordinate so I try this but it didn't work (I get the coordinate BEFORE the rotation) :

QList<QGraphicsItem> group_list = group->childItems();

for(int i=0 ; i < list_rect_item.size() ; i++) {
    list_rect_item.at(i)->setPos(group_list.at(i).pos());
    scene.addItem(list_rect_item.at(i));
}

How can I transform a group of items, and after get all of there new coordinate ? (After the transformation I want to continue to use them not as a group)

È stato utile?

Soluzione

You should call scenePos() function to get the global coordinate of the item after the transformation. Calling pos() function will give you the local coordinate of the item with respect to the parent of the item.

Altri suggerimenti

To ungroup an item group, you can either call QGraphicsScene::destroyItemGroup(), or you can manually remove all items from the group by calling removeFromGroup().

So after grouping and applying transformations you can ungroup it by:

scene.destroyItemGroup(group);

Also the Qt Documentation states that:

The operation of adding and removing items preserves the items' scene-relative position and transformation, as opposed to calling setParentItem(), where only the child item's parent-relative position and transformation are kept.

So after ungrouping you can easily get the position or other properties of the items and continue your desired transformations or actions.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top