Important: Please read the Qt Code of Conduct -

Scene selectedItems() not updated when setSelected(true)

  • 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!!

  • @jsm_d


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

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

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


    Does your item have the selectable flag set?

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

  • @jsm_d

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

    In customItem c'tor:


    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.

    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)))

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

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

Log in to reply