[PyQt] QListWidget item editing

J Barchan jnbarchan at gmail.com
Fri Dec 14 16:12:51 GMT 2018


I have started a thread on the Qt Forum at
https://forum.qt.io/topic/97628/qlistwidget-item-editing.  The people there
are very helpful about how to do things for Qt in C++, but often there
isn't really anyone to help out correctly for PyQt/Python.  I need to
extend what I have done in PyQt, and I think I might need some PyQt help to
sort it out.  Someone here like the excellent & kind @Florian Bruhin
<me at the-compiler.org> will probably be able to sort me out immediately! :)

I won't repeat the whole stuff.  Basically, I was given to translate:

class SignalItemDelegate : public
QStyledItemDelegate{Q_OBJECTQ_DISABLE_COPY(SignalItemDelegate)public:explicit
SignalItemDelegate(QObject* parent =
Q_NULLPTR):QStyledItemDelegate(parent){
QObject::connect(this,&SignalItemDelegate::closeEditor,this,&SignalItemDelegate::editFinished);
}void setEditorData(QWidget *editor, const QModelIndex &index) const
Q_DECL_OVERRIDE{
editStarted();return QStyledItemDelegate::setEditorData(editor,index);
}
Q_SIGNALS:void editStarted();void editFinished();
};

for which I came up with:

class JEditableListStyledItemDelegate(QtWidgets.QStyledItemDelegate):
    # class variable for "editStarted" signal
    editStarted = QtCore.pyqtSignal(name='editStarted')
    # class variable for "editFinished" signal
    editFinished = QtCore.pyqtSignal(name='editFinished')

    def __init__(self, parent: QtCore.QObject=None):
        super().__init__(parent)

        self.closeEditor.connect(self.editFinished)

    def setEditorData(self, editor: QtWidgets.QWidget, index:
QtCore.QModelIndex):
        self.editStarted.emit()
        return super().setEditorData(editor, index)


class JEditableListWidget(QtWidgets.QListWidget):
    # class variable for "editStarted" signal
    editStarted = QtCore.pyqtSignal(name='editStarted')
    # class variable for "editFinished" signal
    editFinished = QtCore.pyqtSignal(name='editFinished')

    def __init__(self, parent: QtWidgets.QWidget=None):
        super().__init__(parent)

        styledItemDelegate = JEditableListStyledItemDelegate(self)
        styledItemDelegate.editStarted.connect(self.editStarted)
        styledItemDelegate.editFinished.connect(self.editFinished)
        self.setItemDelegate(styledItemDelegate)


*My first question is*: is that about right (it does seem to work)?


*My second question is*: if you read my latest
https://forum.qt.io/topic/97628/qlistwidget-item-editing/10, you'll see I
need to introduce a parameter to my signals from my QListWidget to the
outside world for which QListWidgetItem is being edited.  I believe I need
to change my QListWidget signals like:

class JEditableListWidget(QtWidgets.QListWidget):
    # class variable for "editStarted" signal, including item
    editStarted = QtCore.pyqtSignal(QtWidgets.QListWidgetItem,
name='editStarted')

Right?  And then how do I emit that signal?  Is it like:

styledItemDelegate.editStarted.connect(lambda:
self.editStarted.emit(someItemWidget))

?

   - Is that use of a lambda right (that's a Python/PyQt question)?
   - And how do I get at from within the QListWidget which QListWidgetItem
   to pass as someItemWidget (that's probably a Qt question)?


Many thanks to anyone who responds!

-- 
Kindest,
Jonathan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20181214/8864cf72/attachment.html>


More information about the PyQt mailing list