History Drop Down With Model

Following is a bit of python code that illustrates how to create a QComboBox that attaches to a model for listing history items. The main features of this code are items entered in the text area of the combo are added to the history. Selected items and items entered that already appear in the combo are moved to the top. When MAX_ITEMS is exceeded older items (items at the bottom of the drop down) are removed.

#!/usr/bin/env python

from PyQt4.Qt import *
from PyQt4.QtGui import *

class ComboModel(QAbstractListModel):

    MAX_ITEMS = 5
    items = [u'123', u'456', u'789']

    # Required to get a working model.
    def rowCount(self, parent=QModelIndex()):
        # This is a List model meaning all elements are root elements.
        if parent and parent.isValid():
            return 0
        return len(self.items)

    # Required to get a working model.
    def insertRows(self, row, count, parent=QModelIndex()):
        self.beginInsertRows(parent, 0, 1)
        self.endInsertRows()
        return True

    def data(self, index, role):
        if role in (Qt.DisplayRole, Qt.EditRole):
            return QVariant(self.items[index.row()])
        return QVariant()

    def setData(self, index, value, role):
        value = unicode(value.toString())
        if value in self.items:
            # Move the item to the top of the list.
            del self.items[self.items.index(value)]
            self.items.insert(0, value)
        else:
            # Add the new item to the top of the list.
            self.items.insert(0, value)
            self.remove_items()
        self.emit(SIGNAL('dataChanged(QModelIndex, QModelIndex)'),
            self.createIndex(0, 0), self.createIndex(len(self.items) - 1, 0))
        return True

    def remove_items(self):
        '''
        Checks the number of items in the list and if it has been exceeded
        removes extra items.
        '''
        if len(self.items) > self.MAX_ITEMS:
            count = len(self.items) - self.MAX_ITEMS
            del self.items[-count:]

    def order_items(self, index):
        '''
        Move the selected item tot he top of the list.
        '''
        # We only need to move the item to the top if it is not the first item.
        if index > 0:
            self.emit(SIGNAL('layoutAboutToBeChanged()'))
            value = self.items[index]
            del self.items[index]
            self.items.insert(0, value)
            self.emit(SIGNAL('layoutChanged()'))


class MComboBox(QComboBox):

    def __init__(self, parent=None):
        QComboBox.__init__(self, parent)
        self.setEditable(True)
        # The default policy is InsertAtBottom. InsertAtTop must be set
        # otherwise the model will move the item to the top and the QComboBox
        # will select the last index. In the case of reaching max items the
        # index will be invalid because the removal is after the QComboBox has
        # stored the value of the last index.
        self.setInsertPolicy(QComboBox.InsertAtTop)
        # Without this the QComboBox will not call set data for us to check
        # for items already in the model if the item is a duplicate.
        self.setDuplicatesEnabled(True)
        self._model = ComboModel()
        self.setModel(self._model)

        # We need to tell the model when an item is selected so it can be moved
        # to the top of the history list.
        self.connect(self, SIGNAL('activated(int)'), self._model.order_items)


def main():
    app = QApplication([])

    bx = MComboBox()
    bx.show()

    app.exec_()

if __name__ == '__main__':
    main()

One thing to note is that in this example the model stores the items in a list called items. This can be replaced with some other way to retrieve the history items. For example with a connection to an SQLite DB.