无需文本输入立即在 QStyledItemDelegate 中弹出 QCompleter
Popup QCompleter in QStyledItemDelegate immediately without text input
我在 QTableWidget 列上有一个 QCompleter。一旦用户开始编辑,我希望完成者弹出,而不是等待他们先输入文本。我将 QStyledItemDelegate 的 setEditorData 函数子类化来执行此操作,这对我来说似乎最有意义,但是当我调用 completer.complete()
时,在我完成编辑之前没有任何反应(此时弹出窗口触发)。
这是我的委托代码:
class CompleterItemDelegate(QtGui.QStyledItemDelegate):
def createEditor(self, parent, option, index):
completer = QtGui.QCompleter(['test', 'test2'])
completer.setCompletionMode(completer.UnfilteredPopupCompletion)
edit = QtGui.QLineEdit(parent)
edit.setCompleter(completer)
return edit
def setEditorData(self, editor, index):
completer = editor.completer()
completer.complete() # does not fire until after editing is done
completer.popup().show() # no luck here either
print("setting editor data") # this however does work as expected...
super().setEditorData(editor, index)
您必须调用 complete()
when the widget is displayed and for this you can use the showEvent()
方法:
from PyQt4 import QtCore, QtGui
class LineEdit(QtGui.QLineEdit):
def showEvent(self, event):
if self.completer() is not None:
QtCore.QTimer.singleShot(0, self.completer().complete)
super().showEvent(event)
class CompleterItemDelegate(QtGui.QStyledItemDelegate):
def createEditor(self, parent, option, index):
completer = QtGui.QCompleter(["test", "test2"])
completer.setCompletionMode(QtGui.QCompleter.UnfilteredPopupCompletion)
edit = LineEdit(parent)
edit.setCompleter(completer)
return edit
def main(args):
app = QtGui.QApplication(args)
w = QtGui.QTableWidget(4, 4)
delegate = CompleterItemDelegate(w)
w.setItemDelegate(delegate)
w.show()
ret = app.exec_()
return ret
if __name__ == "__main__":
import sys
sys.exit(main(sys.argv))
我在 QTableWidget 列上有一个 QCompleter。一旦用户开始编辑,我希望完成者弹出,而不是等待他们先输入文本。我将 QStyledItemDelegate 的 setEditorData 函数子类化来执行此操作,这对我来说似乎最有意义,但是当我调用 completer.complete()
时,在我完成编辑之前没有任何反应(此时弹出窗口触发)。
这是我的委托代码:
class CompleterItemDelegate(QtGui.QStyledItemDelegate):
def createEditor(self, parent, option, index):
completer = QtGui.QCompleter(['test', 'test2'])
completer.setCompletionMode(completer.UnfilteredPopupCompletion)
edit = QtGui.QLineEdit(parent)
edit.setCompleter(completer)
return edit
def setEditorData(self, editor, index):
completer = editor.completer()
completer.complete() # does not fire until after editing is done
completer.popup().show() # no luck here either
print("setting editor data") # this however does work as expected...
super().setEditorData(editor, index)
您必须调用 complete()
when the widget is displayed and for this you can use the showEvent()
方法:
from PyQt4 import QtCore, QtGui
class LineEdit(QtGui.QLineEdit):
def showEvent(self, event):
if self.completer() is not None:
QtCore.QTimer.singleShot(0, self.completer().complete)
super().showEvent(event)
class CompleterItemDelegate(QtGui.QStyledItemDelegate):
def createEditor(self, parent, option, index):
completer = QtGui.QCompleter(["test", "test2"])
completer.setCompletionMode(QtGui.QCompleter.UnfilteredPopupCompletion)
edit = LineEdit(parent)
edit.setCompleter(completer)
return edit
def main(args):
app = QtGui.QApplication(args)
w = QtGui.QTableWidget(4, 4)
delegate = CompleterItemDelegate(w)
w.setItemDelegate(delegate)
w.show()
ret = app.exec_()
return ret
if __name__ == "__main__":
import sys
sys.exit(main(sys.argv))