So I've subclassed QItemDelegate to add a QCompleter to the QLineEdit widgets that are created for editing a table cell. However, as soon as I type the first letter of a word that's in the completer, the entire table loses focus and the next widget is selected. This _doesn't_ happen if the word isn't in the completer, or the cell is activated for editing.

This seems like buggy behavior to me. I'm wondering if anyone else has come across this or can confirm this, and if anyone might be able to suggest a possible work around. Setting edit triggers to AllEditTriggers mitigates this somewhat, but isn't foolproof. Doing that also seems to make it harder to hook into events to customize the table's focus behavior (which is something else I'm trying to do).

I'm using PyQt 4.7.3 on Debian Linux, but this appears to be the same issue that's described in this thread. Other than that, I haven't found any other resources discussing this.

Here's a code snippet that demonstrates the problem:

Qt Code:
  1. import sys
  2.  
  3. from PyQt4.QtCore import Qt
  4. from PyQt4.QtGui import *
  5.  
  6.  
  7. class TableDelegate(QItemDelegate):
  8.  
  9. def createEditor(self, parent, option, idx):
  10. editor = super(TableDelegate, self).createEditor(parent, option, idx)
  11.  
  12. completer = QCompleter(['fee', 'fie', 'foe', 'fum'], editor.parent())
  13. completer.setCaseSensitivity(Qt.CaseInsensitive)
  14. editor.setCompleter(completer)
  15.  
  16. return editor
  17.  
  18.  
  19. def main():
  20. app = QApplication(sys.argv)
  21.  
  22. table = QTableWidget(2, 4)
  23. table.setItemDelegate(TableDelegate())
  24.  
  25.  
  26. layout = QFormLayout()
  27. layout.addRow(table)
  28. layout.addRow(buttons)
  29.  
  30. view = QDialog()
  31. view.setLayout(layout)
  32. view.show()
  33.  
  34. buttons.accepted.connect(view.accept)
  35. buttons.rejected.connect(view.reject)
  36.  
  37. app.exec_()
  38.  
  39.  
  40. if __name__ == '__main__':
  41. main()
To copy to clipboard, switch view to plain text mode 

Any help would be greatly appreciated!