Important: Please read the Qt Code of Conduct -

How to make instances of custom QGraphicsItem be aware of each other.

  • Lets say I have a custom QGraphicsItem, which has a bool variable determining whether it should be painted blue or red, (its value is taken in the paint function with if statement).

    The change of the bool variable is triggered on certain QKey event only when the item is selected, and this logic resides in the overriden keyPressEvent function. If the key is pressed and the bool is 1, it becomes 0 and vice versa.

    If I make several of those items in a scene, and I multiselect them, I want them to know about each other's bool state. For example if all items are blue (bool is false), and I select all of them and press the key, they should change to red (because the bool changes to 1 and the painter paints on that condition), but if few of the selected ones are already red I want them to stay red, and not change their color to blue.

    Of course this is a very simplified example, the actual code is way more complicated, but let's hope you get the idea. I can try and write a simple code example, if requested.

    Edit: I also have a custom QGraphicsScene, and I also can catch the keypress there, and dispatch it selectively to the items via QList, but the scene has no way of knowing the bool parameters of the items.

  • @Hristo-Konstantinov
    You don't want the objects to know about each other particularly, you want something to query them about their bool state and act accordingly (e.g. counting their colours).

    but the scene has no way of knowing the bool parameters of the items.

    What do you mean by this? There are various ways of allowing it to know.

  • Obviously I'm still a noob lol. But you can still give me some hints if you wish. Now I realize my question doesn't concern the way QT works, but it is more or less derived from the fact that I'm programming for less than 3 months and probably I don't have a very good understanding of how OOP works. I apologize for posting my question here, probably stack overflow would be a better place. If the moderators decide, they can delete the topic.

  • Lifetime Qt Champion

    Hi and welcome to the forums

    Your question is perfectly acceptable here. We mostly ask you try to give a good description,
    show what you have and don't respond with "dont work" for suggestions without providing
    details on what is not working. If you follow these rules, you will find you can ask almost anything
    and we try to help.

    OPP is often about keeping data and processing together in a class to hide the details from the outside world.

    However, in your use case, one could argue that each QGraphicsItem don't really need to know about each other as
    the selection and keypress is actually handled outside of them so strickly that class should control the logic.
    The Scene would not be a bad choice since you already subclassed it so would be fine for it to control this selecting logic.

    The scene can read your bools.
    However, it returns a list of Base pointers,so you must cast them to turn it into your type and hence can access the bool.

    QList<QGraphicsItem*> list = scene->selectedItems();
    for(int i = 0; i < list.length(); i++)
        CustomItem *item = qgraphicsitem_cast<CustomItem*>(list[i]);
        if (item)  // is really not your type, it will be null!
    ** CustomItem  being your custom class name.

  • Haven't read your comment till now. Never heard of casting, but that's something new I might look into.
    However my decision was to create a boolean 2d array in my main widget (one dimension for the number of graphicsitems, and one for their status) and I passed it to arrays of pointers in the scene class and in the graphicsitem class respectively. Now everybody knows everythibg!

  • Lifetime Qt Champion

    Casting is often used as the Scene returns QGraphicsItem * but in reality
    its a mixed list of subclasses of QGraphicsItem.
    So we cast in order to have the right type and access added variables etc compared to the base class.

Log in to reply