我正在使用“Python和Qt快速编程”。在第14章中,我们实现了自己的委托类。其中一个功能称为createEditor。下面是它的代码:createEditor实际执行的代码是什么(使用Python和Qt进行快速编程)
def createEditor(self, parent, option, index):
if index.column() == TEU:
spinbox = QSpinBox(parent)
spinbox.setRange(0, 200000)
spinbox.setSingleStep(1000)
spinbox.setAlignment(Qt.AlignRight|Qt.AlignVCenter)
return spinbox
elif index.column() == OWNER:
combobox = QComboBox(parent)
combobox.addItems(sorted(index.model().owners))
combobox.setEditable(True)
return combobox
elif index.column() == COUNTRY:
combobox = QComboBox(parent)
combobox.addItems(sorted(index.model().countries))
combobox.setEditable(True)
return combobox
elif index.column() == NAME:
editor = QLineEdit(parent)
self.connect(editor, SIGNAL("returnPressed()"),
self.commitAndCloseEditor)
return editor
elif index.column() == DESCRIPTION:
editor = richtextlineedit.RichTextLineEdit(parent)
self.connect(editor, SIGNAL("returnPressed()"),
self.commitAndCloseEditor)
return editor
else:
return QStyledItemDelegate.createEditor(self, parent, option,
index)
下面是commitAndCloseEditor代码:
def commitAndCloseEditor(self):
editor = self.sender()
if isinstance(editor, (QTextEdit, QLineEdit)):
self.emit(SIGNAL("commitData(QWidget*)"), editor)
self.emit(SIGNAL("closeEditor(QWidget*)"), editor)
我明白发生了什么为标准箱,老板,国家和description列。但是,我没有看到NAME列中获得了什么。其实,我出去评论名称代码,这样基本功能将被调用,我看不到任何有什么区别...
请注意,该书有几年了,这意味着当前版本的'PyQt' /'Qt'可能会有不同的行为,那么作者在编写本书时会考虑到这一点。 “NAME”列的用途非常明确:如果用户按下输入,应提交编辑并关闭编辑器。代码是否放在那里,因为较早的版本没有这种行为,或者仅仅是为了统一我不知道的函数,而* nobody *不能回答,除了作者。 – Bakuriu