I'm going through "Rapid Gui Programming With Python and Qt". In chapter 14, we implement our own delegate class. One of the functions is called createEditor. Here's the code for it:
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)
Here's the code for commitAndCloseEditor:
def commitAndCloseEditor(self):
editor = self.sender()
if isinstance(editor, (QTextEdit, QLineEdit)):
self.emit(SIGNAL("commitData(QWidget*)"), editor)
self.emit(SIGNAL("closeEditor(QWidget*)"), editor)
I understand what's happening for the TEU, OWNER, COUNTRY and DESCRIPTION columns. However, I don't see what's being achieved in the NAME column. In fact, I commented the NAME code out so the base function would be called and I can't see any difference whatsoever...
self.connect(editor, SIGNAL("returnPressed()"),
self.commitAndCloseEditor)
What that line does is call Qt's connect
function to create a link between a SIGNAL
, which in this case is returnPressed
, and a SLOT
which is a function and in this case is the commitAndCloseEditor
. Without digging into the code too much it looks like it's there so when the return key is pressed and commitAndCloseEditor
function is triggered to update the data source and close an editor view for the table.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.