createEditor实际做的代码是什么(使用Python和Qt进行Rapid Gui编程)

时间:2014-01-13 16:10:15

标签: python qt user-interface pyqt

我正在经历“使用Python和Qt进行Rapid Gui编程”。在第14章中,我们实现了自己的委托类。其中一个函数叫做createEditor。这是代码:

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)

我了解TEU,OWNER,COUNTRY和DESCRIPTION列的内容。但是,我没有看到在NAME列中实现了什么。事实上,我对NAME代码进行了评论,因此将调用基本函数,我无法看到任何差异......

1 个答案:

答案 0 :(得分:1)

self.connect(editor, SIGNAL("returnPressed()"),
                 self.commitAndCloseEditor)

该行的作用是调用Qt的connect函数来创建SIGNAL(在本例中为returnPressed)与SLOT之间的链接。在这种情况下是commitAndCloseEditor。如果没有深入挖掘代码,它看起来就像那样,当按下返回键并触发commitAndCloseEditor函数来更新数据源并关闭表的编辑器视图时。