CppLearner
CppLearner

Reputation: 17040

How does emit() knows which SLOT to use in PyQT?

in main.py

def setNumberFormat2(self):
    dialog = numberformatdlg2.NumberFormatDlg(self.format, self)
    self.connect(dialog, SIGNAL("changed"), self.refreshTable)
    dialog.show()

and in form.py:

class NumberFormatDlg(QDialog):
  def __init__(self, format, parent=None):
       .....
     self.connect(buttonBox.button(QDialogButtonBox.Apply),
                                  SIGNAL("clicked()"), self.apply)
     self.connect(buttonBox, SIGNAL("rejected()"),
                                  self, SLOT("reject()"))
     self.setWindowTitle("Set Number Format (Modeless)")


  def apply(self):
     ....
     self.emit(SIGNAL("changed"))

According to the author:

Finally, we emit a changed signal, and as we have seen, this causes the caller’s refreshTable() method to be called, which in turn formats all the numbers in the table using the caller’s format dictionary.

When we emit, how does it know we which SLOT we want to apply? Essentially, if we have

self.connect(dialog, SIGNAL("changed"), self.refreshTable)
self.connect(dialog, SIGNAL("changed"), self.anotherMethod)

How does self.emit(...) know which slot we are applying? or am I missing some information?

Thanks.

Upvotes: 1

Views: 454

Answers (1)

iTayb
iTayb

Reputation: 12743

self.emit doesn't "know" anything - it just fires the signal. The self.connect() method is catching the signal and running the given function with the signal parameters.

If you connect two functions to a signal, I think that both will run. You probably shouldn't do it, because it'll make your code a bit unreadable.

  • If you want to run two functions, create a function that is called changed_signal that will call the other functions.
  • If you want to run different functions for different uses, just fire different signals.

Upvotes: 3

Related Questions