Getting an ordered list of ids of selected objects using PyQGIS

Use this script:

selection = []

def selection_changed(selected, deselected, clearAndSelect):
    global selection

    # If the user deselects feature(s), remove ID's from list
    if deselected:
        selection = [ID for ID in selection if ID not in deselected]
        print(f"Selection: {selection}")
        # get newly selected IDs
        new_selected = list(set(selected) - set(selection))

        # if multiple objects are selected, deselect them and warn the user
        if len(new_selected) > 1:
            print("Select just one item!")

        # if one object is selected, add its ID to list
        print(f"Selection: {selection}")


Note that selectionChanged doesn't work all layers. I mean after running the script, if you select another layer, you should run the script for that layer, too.

enter image description here

I do something similiar in my stripchartplugin at

When I initiate, I define an array:


I set up a signal:


and in self.markselected I can do


where self.view.layer is set elsewhere and is the layer I am working on and compare sels with self.selectlines

For my purposes, the sorting of the lists does not matter, but what you can do is probably to

  1. check the length of sels compared to self.selectlines, if it is more than one longer, thrown an error - if it is shorter, well you have to decide. (see below)

  2. if len(sels) - len(self.selectlines) == 1, check which elements in sels that is not in self.selectlines and append it.

The difficulty comes in if a user does something unexpected - they always will do - e.g. if a user has selected 10 items, deselects everything and reselects 11 other items - you need to be able to handle that.

(the solution will probably be to check if there is more than one item that is in sels that is not in self.selectlines, throw an error if so - but how tho check it is more general Python than QGIS)