knack
28th August 2010, 17:41
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 (http://www.qtcentre.org/threads/12677-Completer-on-QItemDelegate). Other than that, I haven't found any other resources discussing this.
Here's a code snippet that demonstrates the problem:
import sys
from PyQt4.QtCore import Qt
from PyQt4.QtGui import *
class TableDelegate(QItemDelegate):
def createEditor(self, parent, option, idx):
editor = super(TableDelegate, self).createEditor(parent, option, idx)
completer = QCompleter(['fee', 'fie', 'foe', 'fum'], editor.parent())
completer.setCaseSensitivity(Qt.CaseInsensitive)
editor.setCompleter(completer)
return editor
def main():
app = QApplication(sys.argv)
table = QTableWidget(2, 4)
table.setItemDelegate(TableDelegate())
buttons = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
layout = QFormLayout()
layout.addRow(table)
layout.addRow(buttons)
view = QDialog()
view.setLayout(layout)
view.show()
buttons.accepted.connect(view.accept)
buttons.rejected.connect(view.reject)
app.exec_()
if __name__ == '__main__':
main()
Any help would be greatly appreciated!
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 (http://www.qtcentre.org/threads/12677-Completer-on-QItemDelegate). Other than that, I haven't found any other resources discussing this.
Here's a code snippet that demonstrates the problem:
import sys
from PyQt4.QtCore import Qt
from PyQt4.QtGui import *
class TableDelegate(QItemDelegate):
def createEditor(self, parent, option, idx):
editor = super(TableDelegate, self).createEditor(parent, option, idx)
completer = QCompleter(['fee', 'fie', 'foe', 'fum'], editor.parent())
completer.setCaseSensitivity(Qt.CaseInsensitive)
editor.setCompleter(completer)
return editor
def main():
app = QApplication(sys.argv)
table = QTableWidget(2, 4)
table.setItemDelegate(TableDelegate())
buttons = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
layout = QFormLayout()
layout.addRow(table)
layout.addRow(buttons)
view = QDialog()
view.setLayout(layout)
view.show()
buttons.accepted.connect(view.accept)
buttons.rejected.connect(view.reject)
app.exec_()
if __name__ == '__main__':
main()
Any help would be greatly appreciated!