1

I'm using PySide2 and not clearly about Signal and Event

If we have two people are doing two View.

Person A is doing ListView

Person B is doing ParameterView

while ListItem be selected, update ParameterView

How should I Connect them? use Signal or Event?

Maybe I would have another View, it needs to be update also, while ListItem selectChanged


Signal

class ListView(QListView):
    # do something

class ParameterView(QWidget):
    def update(self):
        # do something

list_view = ListView()
parameter_view = ParameterView()
list_view.selectChanged.connect(parameter_view.update)

Event

class ListView(QListView):
    def selectChanged(self):
        QApplication.sendEvent(self, SelectChangedEvent)

class SelectChangedEvent(QEvent):
    # initialize ...

class ParameterView(QWidget):
    def update(self):
        # do something

    def event(self, event):
        if event.type() == SelectChangedEvent:
            self.update()

enter image description here

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Bon
  • 88
  • 7

2 Answers2

0

Here you can read a comparison between signals and events and decide what is right for you https://www.learnpyqt.com/courses/start/signals-slots-events/ I believe you better use single and slots to solve this problem but this is your choice

Hope this helped you decide, have a nice day.

yagil
  • 55
  • 1
  • 7
0

Both options are valid since they use the same mechanism to transmit the information but the great difference is that if you want to send a QEvent then you must access the object in that space and instead with the Qt signals you should not know the object in that space but only in the connection.

My recommendation is that signals should be used to decouple classes. I recommend you read:

eyllanesc
  • 235,170
  • 19
  • 170
  • 241