title: How to set text to QLineEdit when it is used as QItemDelegate with QTableView
body: <p>The code below creates a single <code>QTableView</code>. An instance of <code>QAbstractTableModel</code> is then created and assigned to it as its model. Lastly, the <code>QItemDelegate</code> is assigned to the <code>QTableView</code>.</p>
  
  <p>The column 0 is being populated with <code>QLineEdit</code>. While column 1 is populated with <code>QComboBox</code>. 
  But even while <code>QLineEdit</code> is assigned a custom text value the <code>QLineEdit</code> remains to be blank. It doesn't happen with <code>QComboBox</code> which properly gets three items: 'Somewhere', 'Over', 'The Rainbow'. </p>
  
  <p><a href="https://i.stack.imgur.com/9sANB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9sANB.png" alt="enter image description here"></a></p>
  
  <p>What should be done to pre-populate column 0 QLineEdits with the text?</p>
  
  <pre><code>from PyQt4 import QtCore, QtGui
  app = QtGui.QApplication([])
  
  class Delegate(QtGui.QItemDelegate):
      def __init__(self):
          QtGui.QItemDelegate.__init__(self)
  
      def createEditor(self, parent, option, index):
          if index.column()==0:
              lineedit=QtGui.QLineEdit(parent)
              lineedit.setText('Somewhere over the rainbow')
              return lineedit
          elif index.column()==1:
              combo=QtGui.QComboBox(parent)
              combo.addItems(['Somewhere','Over','The Rainbow'])
              combo.setCurrentIndex(index.row())
              return combo
  
  class Model(QtCore.QAbstractTableModel):
      def __init__(self):
          QtCore.QAbstractTableModel.__init__(self)
          self.items = [[1, 'one', None], [2, 'two', None], [3, 'three', None]]
  
      def rowCount(self, parent=QtCore.QModelIndex()):
          return 3 
      def columnCount(self, parent=QtCore.QModelIndex()):
          return 3
  
      def data(self, index, role):
          if not index.isValid(): return
          row = index.row()
          column = index.column() 
          if role == QtCore.Qt.DisplayRole:
              return self.items[row][column]
  
  tableModel=Model()
  tableView=QtGui.QTableView() 
  tableView.setModel(tableModel)
  tableView.setItemDelegate(Delegate())
  
  for row in range(tableModel.rowCount()):
      for column in range(tableModel.columnCount()):
          index=tableModel.index(row, column)
          tableView.openPersistentEditor(index)
  
  tableView.show()
  app.exec_()
  </code></pre>
  
  <h1>EDITED LATER:</h1>
  
  <p>Big thanks Fabio for the trick.</p>
  
  <p>Working code is posted below.</p>
  
  <p>Instead of handling the value assignment inside of <code>Delegate.createEditor()</code> method we should implement <code>Delegate.setEditorData(editor, index)</code> instead.
  Inside of <code>setEditorData</code> we can get both: <code>index.column()</code> and <code>index.row()</code> as well as to access <code>self.items</code> variable used by model using <code>index</code> argument <code>value = index.model().items[row][column]</code></p>
  
  <p><a href="https://i.stack.imgur.com/cszYK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cszYK.png" alt="enter image description here"></a></p>
  
  <pre><code>from PyQt4 import QtCore, QtGui
  app = QtGui.QApplication([])
  
  class Delegate(QtGui.QItemDelegate):
      def __init__(self):
          QtGui.QItemDelegate.__init__(self)
  
      def createEditor(self, parent, option, index):
          if index.column()==0:
              lineedit=QtGui.QLineEdit(parent)
              return lineedit
  
          elif index.column()==1:
              combo=QtGui.QComboBox(parent)
              return combo
  
      def setEditorData(self, editor, index):
          row = index.row()
          column = index.column()
          value = index.model().items[row][column]
          if isinstance(editor, QtGui.QComboBox):
              editor.addItems(['Somewhere','Over','The Rainbow'])
              editor.setCurrentIndex(index.row())
          if isinstance(editor, QtGui.QLineEdit):
              editor.setText('Somewhere over the rainbow')
  
  class Model(QtCore.QAbstractTableModel):
      def __init__(self):
          QtCore.QAbstractTableModel.__init__(self)
          self.items = [[1, 'one', 'ONE'], [2, 'two', 'TWO'], [3, 'three', 'THREE']]
  
      def flags(self, index):
          return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEditable
      def rowCount(self, parent=QtCore.QModelIndex()):
          return 3 
      def columnCount(self, parent=QtCore.QModelIndex()):
          return 3
  
      def data(self, index, role):
          if not index.isValid(): return 
          row = index.row()
          column = index.column()
          if role == QtCore.Qt.DisplayRole or role == QtCore.Qt.EditRole:
              return self.items[row][column]
  
  tableModel=Model()
  tableView=QtGui.QTableView() 
  tableView.setModel(tableModel)
  tableView.setItemDelegate(Delegate())
  
  for row in range(tableModel.rowCount()):
      for column in range(tableModel.columnCount()):
          index=tableModel.index(row, column)
          tableView.openPersistentEditor(index)
  
  tableView.show()
  app.exec_()
  </code></pre>