Is there a way to limit the number of items my QTreeWidget can have, similar to a FIFO buffer ?
I am still not entirely sure what it is you want to implement. But here is some general advice.
If you want to achieve custom constraints and behavior for interaction of a widget with an underlying data structure, you should consider following Qt's Model View Architecture.
Any multiple item displaying QWidget can be replaced by a QView + QModel. You could for instance replace QTreeWidget
with QTreeView
. Then you implement your own model derived from QAbstractItemModel
and attach it to the view. Views will automatically adapt to changes made in the model. In that way, you could for instance have a container in the model class and whenever an item is added you check if the maximum number is reached and then remove whatever other element.
These tutorials will give you a deeper understanding of what I am suggesting. I know they are C++, but all of it should be easily translated to pyqt.