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. Scene selectedItems() not updated when setSelected(true)
Forum Updated to NodeBB v4.3 + New Features

Scene selectedItems() not updated when setSelected(true)

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 2 Posters 1.1k 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.
  • J Offline
    J Offline
    jsm_d
    wrote on last edited by
    #1

    Hi,

    I have a scene with several custom graphicItems and selection and moving is managed by mouse events of the scene. When I select an item with mouse pulsation, scene selectedItems vector updates ok.

    If I add a new Item with addItem from a new pointer to the scene and after that I make item->setSelected(true), the item seems to be selected (is painted with anchors if selected in items paint function), but the Item is not included in the selectedItems list of the scene.

    Is there any way to force the inclusion of this new item in the selected items of the scene in order the scene to notice that the newly added is selected programatically?

    Thank you!!

    Pl45m4P 1 Reply Last reply
    0
    • J jsm_d

      Hi,

      I have a scene with several custom graphicItems and selection and moving is managed by mouse events of the scene. When I select an item with mouse pulsation, scene selectedItems vector updates ok.

      If I add a new Item with addItem from a new pointer to the scene and after that I make item->setSelected(true), the item seems to be selected (is painted with anchors if selected in items paint function), but the Item is not included in the selectedItems list of the scene.

      Is there any way to force the inclusion of this new item in the selected items of the scene in order the scene to notice that the newly added is selected programatically?

      Thank you!!

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by
      #2

      @jsm_d

      Hi,

      there is a selectionChanged() signal, but it should get triggered as you add a new pre-selected item to the scene.

      https://doc.qt.io/qt-5/qgraphicsscene.html#selectionChanged


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      J 1 Reply Last reply
      0
      • Pl45m4P Pl45m4

        @jsm_d

        Hi,

        there is a selectionChanged() signal, but it should get triggered as you add a new pre-selected item to the scene.

        https://doc.qt.io/qt-5/qgraphicsscene.html#selectionChanged

        J Offline
        J Offline
        jsm_d
        wrote on last edited by
        #3

        @Pl45m4, thanks for the reply.

        The Scene::selectionChanged signal is connected to a slot in the scene. Every mouse selection triggers that signal and qDebug inside this slot shows that selectedItems is updated ok. however, If the item is selected programatically right after adding it, it seems that this signal is not triggered and the slot not executed. selecteditems() is neither updated in that case.

        The items are selectable, so my guess is that the scene is not notified for any reason about the new selection when the selection is done with setSelected(true), but I can´t find why...

        Pl45m4P 1 Reply Last reply
        0
        • J jsm_d

          @Pl45m4, thanks for the reply.

          The Scene::selectionChanged signal is connected to a slot in the scene. Every mouse selection triggers that signal and qDebug inside this slot shows that selectedItems is updated ok. however, If the item is selected programatically right after adding it, it seems that this signal is not triggered and the slot not executed. selecteditems() is neither updated in that case.

          The items are selectable, so my guess is that the scene is not notified for any reason about the new selection when the selection is done with setSelected(true), but I can´t find why...

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by Pl45m4
          #4

          @jsm_d

          Have you read this? It should notify the scene.

          This signal is emitted by QGraphicsScene whenever the selection changes. You can call selectedItems() to get the new list of selected items.
          
          The selection changes whenever an item is selected or unselected, a selection area is set, cleared or otherwise changed, if a preselected item is added to the scene, or if a selected item is removed from the scene.
          

          (https://doc.qt.io/qt-5/qgraphicsscene.html#selectionChanged)

          Does your item have the selectable flag set?


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          J 1 Reply Last reply
          0
          • Pl45m4P Pl45m4

            @jsm_d

            Have you read this? It should notify the scene.

            This signal is emitted by QGraphicsScene whenever the selection changes. You can call selectedItems() to get the new list of selected items.
            
            The selection changes whenever an item is selected or unselected, a selection area is set, cleared or otherwise changed, if a preselected item is added to the scene, or if a selected item is removed from the scene.
            

            (https://doc.qt.io/qt-5/qgraphicsscene.html#selectionChanged)

            Does your item have the selectable flag set?

            J Offline
            J Offline
            jsm_d
            wrote on last edited by
            #5

            @Pl45m4

            Yes, ItemIsSelectable flag is set in the constructor of the item. Everythig you indicated from documentation works fine, except "if a preselected item is added to the scene". It is not triggering the signal in this case (in any case neither setSelected after or before adding to scene). Updating scene seems also not work to update selecteditems.

            Is neither triggering GraphicsItemChange::ItemSelectedChange in itemChage method of the custom item. I can see in this method when the scene is modified, when the new item is added,... but is not notifying the selection change. I have try a lot of different things but unsuccesfully.

            Pl45m4P 1 Reply Last reply
            0
            • J jsm_d

              @Pl45m4

              Yes, ItemIsSelectable flag is set in the constructor of the item. Everythig you indicated from documentation works fine, except "if a preselected item is added to the scene". It is not triggering the signal in this case (in any case neither setSelected after or before adding to scene). Updating scene seems also not work to update selecteditems.

              Is neither triggering GraphicsItemChange::ItemSelectedChange in itemChage method of the custom item. I can see in this method when the scene is modified, when the new item is added,... but is not notifying the selection change. I have try a lot of different things but unsuccesfully.

              Pl45m4P Offline
              Pl45m4P Offline
              Pl45m4
              wrote on last edited by Pl45m4
              #6

              @jsm_d

              Show your code, what you are actually doing then.
              I've tested it myself and it worked

              In customItem c'tor:

              setFlag(ItemIsSelectable);
              setSelected(true);
              

              In my custom scene class c'tor:

                  connect(this, &QGraphicsScene::selectionChanged, [=](){
                      qDebug() << selectedItems();
                  });
              

              and I add them with:

              // In my project where I've tested it, I store my items in another QList, so I pass an (QList) iterator
              // but this doesnt matter. Works anyway.
              addItem((*m_it));
              

              I add two items, console prints:

              // one item added -> size of list = 1
              (CustomItem(0x218e2370, pos=552,108, flags=(ItemIsSelectable)))
              
              // second item added -> size = 2 -> detects 2 selected items
              (CustomItem(0x218e2630, pos=169,399, flags=(ItemIsSelectable)), CustomItem(0x218e2370, pos=552,108, flags=(ItemIsSelectable)))
              

              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

              ~E. W. Dijkstra

              J 1 Reply Last reply
              1
              • Pl45m4P Pl45m4

                @jsm_d

                Show your code, what you are actually doing then.
                I've tested it myself and it worked

                In customItem c'tor:

                setFlag(ItemIsSelectable);
                setSelected(true);
                

                In my custom scene class c'tor:

                    connect(this, &QGraphicsScene::selectionChanged, [=](){
                        qDebug() << selectedItems();
                    });
                

                and I add them with:

                // In my project where I've tested it, I store my items in another QList, so I pass an (QList) iterator
                // but this doesnt matter. Works anyway.
                addItem((*m_it));
                

                I add two items, console prints:

                // one item added -> size of list = 1
                (CustomItem(0x218e2370, pos=552,108, flags=(ItemIsSelectable)))
                
                // second item added -> size = 2 -> detects 2 selected items
                (CustomItem(0x218e2630, pos=169,399, flags=(ItemIsSelectable)), CustomItem(0x218e2370, pos=552,108, flags=(ItemIsSelectable)))
                
                J Offline
                J Offline
                jsm_d
                wrote on last edited by
                #7

                @Pl45m4

                I want to select programatically a particular newlly created item in a particular circumstance, and that is when i realized that this is happening.

                If I reproduce that yos have done, setSelected(true) in the constructor, all off hte items have the same behaviour. When I create the item, is is aparently selected but the signal is not triggered and selectedItems is empty. I can see every items drawn as selected but the list is empty. Is there a globla flag or somethig that only notifies the selectionchange when the selection is done with mouse??. It seems that if the selection is made with setSelected(true) from code the situation is happening allways. But the fact that I can see the items drawn as selected meand that in any point the items are notified as selected, but the scene not.

                Pl45m4P 1 Reply Last reply
                0
                • J jsm_d

                  @Pl45m4

                  I want to select programatically a particular newlly created item in a particular circumstance, and that is when i realized that this is happening.

                  If I reproduce that yos have done, setSelected(true) in the constructor, all off hte items have the same behaviour. When I create the item, is is aparently selected but the signal is not triggered and selectedItems is empty. I can see every items drawn as selected but the list is empty. Is there a globla flag or somethig that only notifies the selectionchange when the selection is done with mouse??. It seems that if the selection is made with setSelected(true) from code the situation is happening allways. But the fact that I can see the items drawn as selected meand that in any point the items are notified as selected, but the scene not.

                  Pl45m4P Offline
                  Pl45m4P Offline
                  Pl45m4
                  wrote on last edited by
                  #8

                  @jsm_d

                  You said that before.
                  If you don't show what you've done, one can only guess what's happening.

                  @jsm_d said in Scene selectedItems() not updated when setSelected(true):

                  Is there a globla flag or somethig that only notifies the selectionchange when the selection is done with mouse??

                  There is no difference between selecting by code or with mouse. When using your mouse, there is only the mouse click event, that is happening before. But for the item, it's the same.


                  If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                  ~E. W. Dijkstra

                  1 Reply Last reply
                  2

                  • Login

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