Search code examples
pythonparameterspyqt4signals-slots

How to emit signals with parameters


My first question about this topic was QtCore.SIGNALS not working on my code.

But how to run this function two times with different parameters? For example:

first time argument = 0

n[0]

second time argument = 1

n[1]

    def view_splash(self, argument):
        print('test')
        label = QLabel("<font color=red size=10<b>" + n[argument] + "</b></font>")
        label.setWindowFlags(Qt.SplashScreen | Qt.WindowStaysOnTopHint)
        label.show()
        QtCore.QTimer.singleShot(5000, label.close)


class AThread(QtCore.QThread):
    trigger = QtCore.pyqtSignal()

    def run(self):
        print('n[0]')
        self.trigger.emit()
        time.sleep(10)
        print('n[1]')
        self.trigger.emit()

Solution

  • Define the signal with the parameter types you want to send:

    class AThread(QtCore.QThread):
        trigger = QtCore.pyqtSignal(int)
    

    Then emit the actual values:

        def run(self):
            print('n[0]')
            self.trigger.emit(0)
            time.sleep(10)
            print('n[1]')
            self.trigger.emit(1)