title: How to create combo-box QItemDelegate
body: <p>I implemented the following delegate, to provide a combobox in a QTableView. The use case is to replace a column (key) that is generally meaningless for the user (e.g. a numerical id) with a text equivalent.</p>
  
  <p>The snippet below works (also for saving the proper value), but it has three issues:</p>
  
  <ol>
  <li>It displays the original value, instead of the text equivalent.</li>
  <li>A selection of rows in the QTableView provides all columns but not the one with this delegate.</li>
  <li>Ideally, I would like the combobox to appear as such, without the user having to click on it to find out it is one.</li>
  </ol>
  
  <p><strong>Note:</strong> The key can be any string (not necessarily an integer number). <em>A typical example would be the country (the value "France" corresponds to the key "FR").</em></p>
  
  <pre class="lang-python prettyprint-override"><code>class ComboDelegate(QtGui.QItemDelegate):
      """
      A delegate that places a QComboBox in every
      cell of the column to which it is being applied
      """
  
      def __init__(self, parent, value_data):
          "Value_data is a list of tuples: (item, label)"
          QtGui.QItemDelegate.__init__(self, parent)
          self.value_data = value_data
  
      @property
      def items(self):
          "The list of items for display"
          return [item[0] for item in self.value_data]
  
      @property
      def labels(self):
          "The list of labels for display"
          return [item[1] for item in self.value_data]
  
      def item(self, label):
          "Get the item from a label"
          try:
              index = self.labels.index(label)
          except ValueError:
              pass
          print("Value no:  &amp;%s" % index)
          return self.items[index]
  
      def createEditor(self, parent, option, index):
          "Create the editor (called each time)"
  
          combo = QtGui.QComboBox(parent)
          for duplet in self.value_data:
              # the duplet is label, item
              item, label = duplet
              combo.addItem(label)
  
          combo.currentIndexChanged.connect(self.currentIndexChanged)
          return combo
  
      def setEditorData(self, editor, index):
          editor.blockSignals(True)
          editor.setCurrentIndex(index.row())
          editor.blockSignals(False)
  
      def setModelData(self, editor, model, index):
          "This is the data stored into the field"
          print("Current text: %s" % editor.currentText())
          model.setData(index, self.item(editor.currentText()))
  
      def currentIndexChanged(self):
          self.commitData.emit(self.sender())
  </code></pre>