I’ve read some examples on how to define a custom model for a QComboBox widget.
Here’s how I defined my model:
class LevelListModel(QAbstractListModel):
def __init__(self, parent=None, *args):
""" datain: a list where each item is a row
"""
QAbstractListModel.__init__(self, parent, *args)
self.levelList = []
def rowCount(self, parent=QModelIndex()):
return len(self.levelList)
def data(self, index, role):
if index.isValid() and role == Qt.DisplayRole:
return QVariant(index.row())
else:
return QVariant()
def addLevel(self,level):
self.beginResetModel()
self.levelList.append(level)
self.endResetModel()
I set the model to my QComboBox:
self.levelListModel = LevelListModel()
self.ui.levelComboBox.setModel(self.levelListModel)
I add a model to my list this way:
newLevel = Level (self.levelListModel.rowCount() + 1)
self.levelListModel.addLevel(newLevel)
The item is added correctly and I can see it inside the combobox, but I would like to change the currentIndex to be the new item’s index.
I guess QAbstractListModel could raise some kind of events that QComboBox can listen to, but I haven’t still found how to do that.
My questions are:
- How can I notify the
QComboBoxthat model data changed, and listen to that event to modify currentIndex accordingly? - I used
[begin|end]ResetModelbecause my entry should be an ordered sequence of integer. So I need to rebuild the data list completely once an item in the middle of the list have been removed. I don’t know if this is the right way to go. Any better solution?
1
No need to listen an event from the way you do things. You know when the model data is changed, because you add things yourself. Just change the
currentIndexafter adding a data.I’d probably modify the
addLevelmethod to return theQModelIndexof the added item and then use it to set thecurrentIndexof theQComboBox:and
2
That depends.
[begin|end]ResetModelis for really drastic changes. I don’t see how keeping an ordered list of integers would lead to such changes for single item addition/removal. From what you describe, you should be using[begin|end]InsertRowsand[begin|end]RemoveRows.