[PyQt] QListWidget item editing

J Barchan jnbarchan at gmail.com
Sat Dec 15 10:59:39 GMT 2018


On Fri, 14 Dec 2018 at 21:22, Maurizio Berti <maurizio.berti at gmail.com>
wrote:

> [sending again as I forgot to reply to the list]
>
> > is that about right (it does seem to work)?
>
> Seems right.
> The "name" argument of signals is not required as long as you use the same
> name for the attribute.
>
>
> > 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)?
>
> Not exactly. If you need to pass an argument to a function within a
> lambda, it has to be in the lambda arguments too, otherwise python will try
> to look for the argument name in the local scope and eventually raise an
> error if it doesn't exist.
>
> Seeing your implementation I'd modify the original editStarted signal of
> the delegate and add a QModelIndex argument:
>
> editStarted = QtCore.pyqtSignal(QtCore.QModelIndex)
>
> then emit the signal from the setEditorData method with its index argument:
>
> self.editStarted.emit(index)
>
> At this point you can connect it with this lambda in the QListWidget init:
>
> styledItemDelegate.editStarted.connect(lambda index: self.editStarted.emit
> (self.itemFromIndex(index))
>
> In this way the the lambda will receive the index argument previously
> emitted, convert it to a list widget item and finally emit its own
> editStarted signal with the correct list widget item argument.
>
> Regards,
> Maurizio
>
> --
> È difficile avere una convinzione precisa quando si parla delle ragioni
> del cuore. - "Sostiene Pereira", Antonio Tabucchi
> http://www.jidesk.net
>

@Maurizio Berti
Thank you so much for replying.  I get the gist now.  I will try
implementing next week!

One thing I didn't like/expect: your Qt reply is that same as at the forum
(so I accept that's the way to do it now), you use the QStyledItemDelegate to
get the item being edited (from the QIndex parameter), I expected that to
be doable in the QListWidget class by (somehow) it knowing which item was
being edited instead, but it seems that's not the case.

Grazie/Arrivederci/Ciao :)

-- 
Kindest,
Jonathan

<https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
Virus-free.
www.avast.com
<https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
<#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20181215/12db9d45/attachment.html>


More information about the PyQt mailing list