[PyQt] QTableWidgetItem editingFinished

simozack simozack at yahoo.it
Tue Jul 7 09:58:57 BST 2009


2009/7/6 Mario Daniel Carugno <carugnom at gmail.com>:

>> It works exactly like the QLineEdit, because the widget it calls IS a
>> QLineEdit (really not always: if it is a number it calls a QSpinBox or
>> a QDoubleSpinBox, if it is a date a QDateEdit or a QDateTimeEdit
>> etc...).
>>
>> The widget is returned by the function QTableWidget.itemDelegate().
>>
> Thank you for help. Do that mean that i must create a model to handle
> that ?

I think it's not necessary. For example, I write for a project of mine
this ViewDelegate class:

class ViewDelegate(QItemDelegate):
    def __init__(self, parent = None):
        super(ViewDelegate, self).__init__(parent)
        self.parent = parent

    def createEditor(self, parent, option, index):
        editor = QItemDelegate.createEditor(self, parent, option, index)
        if isinstance(editor, QLineEdit) and index.column() in (4, 5):
            self.connect(editor, SIGNAL("editingFinished()"),
                   self.commitAndCloseEditor)
        elif isinstance(editor, QLineEdit) and index.column() == 2:
            self.connect(editor, SIGNAL("editingFinished()"),
                    self.parent.setTip)
        return editor

    def commitAndCloseEditor(self):
        editor = self.sender()
        # this signal is emitted for saving the data into the model
        self.emit(SIGNAL("commitData(QWidget*)"), editor)
        # this signal is emitted to close the editor widget
        self.emit(SIGNAL("closeEditor(QWidget*)"), editor)
        #do some action

In the init code of the window I have called:

self.view.setItemDelegate(ViewDelegate(self))

where self.view is the QTableView and self is the parent (a QMainWindow class).

> I'm a newbie yet, it sounds very difficult to me. Anyway, I guess i need
> to learn more on that topic, thank you Simozack !

IMHO it's not so difficult once you have understood the logic behind
the scene of QT.

HTH,
Simone


More information about the PyQt mailing list