0

I'm trying to create a version of QListWidget (in PySide) in which the itemClicked signal will carry not one item, but a list of all items in the QListWidget.

I tried different things, but no luck so far. This is what I have ATM:

class RelationsListWidget(QListWidget):
    all_items = Signal(list)
    item_list = []

    def __init__(self):
        QListWidget.__init__(self)
        self.itemClicked.connect(self.gather_items)

    def gather_items(self):
        self.item_list = [self.item(i) for i in range(self.count())]
        self.all_items.emit(self.item_list)

but when I connect it:

class NodeEditWindow(QDialog):
   ...
   self.list_consumes = RelationsListWidget()
   self.list_consumes.itemClicked.connect(self.create_SelectRelationsWindow)
   ...
   @Slot(object)
   def create_SelectRelationsWindow(self, list_widget):
       print("create_SelectRelationsWindow: %s" % type(list_widget))

I'm getting:

create_SelectRelationsWindow: <class '__main__.NodeItem'> 

so it carries only one item, not a list.


related questions:

How to connect custom signal to slot in pyside with the new syntax?

PyQt4.QtCore.pyqtSignal object has no attribute 'connect'

Community
  • 1
  • 1
robaki
  • 101
  • 2
  • What is the point of sending a list of all the items? Why not just send a reference to the list-widget itself? Or maybe you don't even need to do that, if the receiver already has a reference. – ekhumoro Sep 16 '16 at 16:50
  • Sending a reference is fine, but I don't know how to do it either. The receiver doesn't have a reference, at least AFAIK (I'm new to PySide). Can the signal receiving object tell where did the signal came from? – robaki Sep 16 '16 at 18:32
  • A "reference" just means any variable that points to the object. In your example code, the receiver clearly *does* have a reference to the list-widget - i.e. `self.list_consumes`. So there's no need to send anything in the signal - just pull the items directly from the list-widget. – ekhumoro Sep 16 '16 at 19:13
  • damn, you're right, of course the `NodeEditWindow` widget does have a reference. There is a problem, though: there will be a few other lists apart from the `self.list_consumes` connected to that slot. How will the Slot method know which list did it get the signal from? (items are not unique, the same item can be on multiple lists). – robaki Sep 16 '16 at 20:43
  • You can use `self.sender()` in the slot. – ekhumoro Sep 16 '16 at 21:25
  • thanks a lot! that solves the problem – robaki Sep 17 '16 at 07:36

0 Answers0