簡體   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