[PyQt] QComboBox and Models

Martin Höfling martin.hoefling at gmx.de
Tue Feb 26 11:51:43 GMT 2008


Hi all,

i have a QComboBox with a model as content. I checked the data() method of my 
model and it should properly return a QVariant(QString(myentry))

It seems as if obtaining the values is fine, since auto-completition if I type 
inside works, but if I wanna use the dropdown menu (set the maxVisibleEntries 
to 5), it shows no entries. I don't want to store any changes made in the 
combobox since I just use it's content (any string) as a filter for another 
model, so I omitted the setData method, is that correct?

Any ideas what could be wrong?

Best wishes
	Martin

Here's the model implementation.

class NameListModel(QAbstractListModel):
    def __init__(self):
        super (NameListModel,self).__init__()
        self.list=[]
    
    def rowCount(self, index=QModelIndex() ):
        return len(self.list)
    
    def data(self,index,role=Qt.DisplayRole):
        if not index.isValid() or not (0<=index.row() < len(self.list)):
            return QVariant()
        if role == Qt.DisplayRole or role == Qt.EditRole:
            return QVariant(self.list[index.row()])
        else:
            return QVariant(self.list[index.row()])
            
        
    def headerData(self, section, orientation, role=Qt.DisplayRole):
        if role == Qt.TextAlignmentRole:
            if orientation == Qt.Horizontal:
                return QVariant(int(Qt.AlignLeft|Qt.AlignVCenter))
            return QVariant(int(Qt.AlignRight|Qt.AlignVCenter))
        
        if role != Qt.DisplayRole:
            return QVariant()
            
    def insertRows(self,position, rows=1, index=QModelIndex()):
        self.beginInsertRows(QModelIndex(),position, position+rows-1)
        
        for row in range(rows):
            self.list.insert(position+row-1,QString())
        
        self.endInsertRows()
        return True
    
    def removeRows(self,position,rows=1, index=QModelIndex()):
        self.beginRemoveRows(QModelIndex(),position,position+rows-1)
                    
        self.list = self.list[:position]+self.list[position+rows:]
        
        self.endRemoveRows()
        return True
        
    def getQStringList(self):
        retl=QStringList()
        for strg in self.list:
            retl.append(strg)
        
    def addString(self, string):
        strg=QString(string)
        for i in range(len(self.list)):
            if strg==self.list[i]:
                return
        self.insertRows(0)
        self.list[0]=strg
        self.list.sort()
    
    def removeString(self, string):
        strg=QString(string)
        for i in range(len(self.list)):
            if strg==self.list[i]:
                self.removeRows(i)
                return


More information about the PyQt mailing list