在 QStyledItemDelegate 中使用信号 closeEditor 的正确方法?
我正在重写 QStyledItemDelegate 类并重新实现 eventFilter 函数,以便我可以在检测到 Tab 按下时自定义编辑器行为。但是,以下内容不起作用。调用 closeEditor 信号的正确方法是什么?
class CustomDelegate(QStyledItemDelegate):
def __init__(self, parent=None):
super(CustomDelegate, self).__init__(parent)
def eventFilter(self, editor, event):
if (event.type() == QEvent.KeyPress and
event.key() == Qt.Key_Tab):
print "Tab captured in editor"
self.commitData.emit(editor) #This is working
self.closeEditor.emit(editor) #This does not seem to do anything??
return True
return QStyledItemDelegate.eventFilter(self,editor,event)
I am overriding the QStyledItemDelegate class and reimplementing the eventFilter function so I can customize the editor behavior when a Tab press is detected. However, the following is not working. What is the correct way to invoke the closeEditor signal?
class CustomDelegate(QStyledItemDelegate):
def __init__(self, parent=None):
super(CustomDelegate, self).__init__(parent)
def eventFilter(self, editor, event):
if (event.type() == QEvent.KeyPress and
event.key() == Qt.Key_Tab):
print "Tab captured in editor"
self.commitData.emit(editor) #This is working
self.closeEditor.emit(editor) #This does not seem to do anything??
return True
return QStyledItemDelegate.eventFilter(self,editor,event)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个老问题,但我刚刚遇到同样的问题并发现了这个问题。
我通过将
self.closeEditor.emit(editor)
行更改为
self.closeEditor.emit(editor, QAbstractItemDelegate.NoHint) 解决了这个问题。
commitData
调用将setModelData
。如果您不调用closeEditor
,则当编辑器本身关闭时,将再次调用setModelData
。This is an old question, but I just ran into the same issue and found this question.
I solved it by changing the
self.closeEditor.emit(editor)
line to
self.closeEditor.emit(editor, QAbstractItemDelegate.NoHint)
.The
commitData
call willsetModelData
. If you don't callcloseEditor
,setModelData
will be called again as the editor itself will close.