2014-01-13 68 views
0

我正在使用“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列中获得了什么。其实,我出去评论名称代码,这样基本功能将被调用,我看不到任何有什么区别...

+0

请注意,该书有几年了,这意味着当前版本的'PyQt' /'Qt'可能会有不同的行为,那么作者在编写本书时会考虑到这一点。 “NAME”列的用途非常明确:如果用户按下输入,应提交编辑并关闭编辑器。代码是否放在那里,因为较早的版本没有这种行为,或者仅仅是为了统一我不知道的函数,而* nobody *不能回答,除了作者。 – Bakuriu

回答

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

什么该行所做的就是调用Qt的connect函数来创建之间的联系SIGNAL,在这种情况下是returnPressed,而SLOT是一种功能,在这种情况下是commitAndCloseEditor。如果没有深入挖掘代码,它看起来就像是在那里,所以当按下回车键并触发commitAndCloseEditor函数来更新数据源并关闭表格的编辑器视图时。

+0

我明白这一点。但是,经过进一步测试,似乎并没有取得任何成就。如果我评论“NAME”部分,它的作用完全相同。如果我将commitAndCloseEditor更改为仅传递,它仍然可以完全相同......我无法理解基本函数无法实现的代码的实现情况。 –

+0

注意'NAME'和'DESCRIPTION'是如何相同的?我想如果你双击'NAME'或'DESCRIPTION'字段,你会得到一个编辑框来改变内容。如果不看这个例子,就无法说出来。 –

相关问题