繁体   English   中英

createEditor的这段代码实际上在做什么(使用Python和Qt进行快速Gui编程)

[英]What is this code from createEditor actually doing (Rapid Gui Programming With Python and Qt)

我正在经历“使用Python和Qt进行快速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代码,因此将调用基本函数,而且我看不到任何区别...

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

该行所做的是调用Qt的connect函数,以在SIGNAL (在本例中为returnPressed )和SLOT即函数)之间建立链接,在本例中为commitAndCloseEditor 无需过多地研究代码,它看起来就在那里,因此当按下返回键并触发commitAndCloseEditor函数来更新数据源并关闭表的编辑器视图时,就可以了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM