I do something similiar in my stripchartplugin at https://github.com/sickel/qgisstripchart/blob/master/stripchart.py
When I initiate, I define an array:
self.selectlines=[]
I set up a signal:
self.iface.mapCanvas().selectionChanged.connect(self.markselected)
and in self.markselected I can do
sels=self.view.layer.selectedFeatures()
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
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)
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)