0
votes

Is there a way to limit the number of items my QTreeWidget can have, similar to a FIFO buffer ?

1
When you say FIFO buffer, I am guessing you are talking about a Queue, which is one dimensional. A tree widget maintains a tree view, displaying a tree model. By nature it might have multiple levels, each containing multiple items. It is therefore multi dimensional. What exactly are you trying to achieve? Are you trying to limit the total number of elements, the number of children an element can have, the number of siblings on a tree level, or something entirely different? What is supposed to happen if you exceed your constraint?Basti Vagabond
I am trying to create a tree which will function as a trace. The problem is that if I keep populating the tree and it will have many items in it, the GUI will start working slow and eventually crash. I think that by restricting the number of items the tree can hold (lets say 2000), the GUI will work much better. So yes, I'm trying to limit the total number of elements in the tree somehowGarjy

1 Answers

2
votes

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.