How to select QGraphicsItem without selecting the whole group (to which this element belongs)

I wonder if it’s possible to select an element belonging to one group without selecting an entire group.

What I'm trying to do is a group of elements that can be moved (all elements are moved together), but none of them can be moved individually. But I also want to be able to select elements within this group, and then do something with them, for example, reorder, delete, change color, etc.

Another problem is that there may be a group of groups. I also want to be able to select these groups within the parent group. I thought this could be a problem because we cannot determine which group users want to select. For demonstration purposes, we say that we have a graphic scene consisting of the following elements:

Group A, which contains group B. Group B contains 3 lines. If I double-click on group A, then I can select group B. If I double-click on B again, then I can select the lines inside group B. Pressing Esc does the opposite, like double-clicking. The problem is that I don’t know how to do it, because the whole choice is sent to the parents, and not back to all his children.

+3
source share
1 answer

You cannot use QGraphicsItemGroup for this, because selecting one item in a group automatically selects all the other memebrs of the group.

I would subclass QGraphicsItem to create a RootItem class. This class will contain two lists. One of them is a list of regular QGraphicsItems, which make up the visual elements of the group itself, born on this RootItem. Another list has other RootItems that are parents of this (e.g. group B).

You just need to make sure that you carefully control which elements can be selected at any given time and how the interaction spreads between the elements.

Hope this helps.

0
source

Source: https://habr.com/ru/post/1772015/


All Articles