Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to make instances of custom QGraphicsItem be aware of each other.
Qt 6.11 is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 1.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hristo Konstantinov
    wrote on last edited by Hristo Konstantinov
    #1

    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.

    JonBJ 1 Reply Last reply
    0
    • H Hristo Konstantinov

      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.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @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.

      1 Reply Last reply
      0
      • H Offline
        H Offline
        Hristo Konstantinov
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          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!
                 item->GetYourBool()
          }
          
          ** CustomItem  being your custom class name.
          
          1 Reply Last reply
          1
          • H Offline
            H Offline
            Hristo Konstantinov
            wrote on last edited by
            #5

            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!

            mrjjM 1 Reply Last reply
            0
            • H Hristo Konstantinov

              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!

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Hristo-Konstantinov
              Hi
              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.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved