Search code examples
pythonmultithreadingpyqtpyqt5qt-signals

pyqt attribute error when trying to emit integer signal


This is probably a stupid mistake somewhere on my part; when I try to implement this threading code to update a PyQt5 gui's progress bar level, I get the following error:

Traceback (most recent call last):
  File "threadtest.py", line 62, in <module>
    app=MainWindow()
  File "threadtest.py", line 35, in __init__
    self.threadclass = ThreadClass()
  File "threadtest.py", line 49, in __init__
    self.getLevels()
  File "threadtest.py", line 53, in getLevels
    self.battery.emit()
AttributeError: 'int' object has no attribute 'emit'
[1:1:0100/000000.677780:ERROR:broker_posix.cc(43)] Invalid node channel message

the code is:

class MainWindow(QtWidgets.QMainWindow, gs.Ui_MainWindow):
    def __init__(self, parent = None):
        super(MainWindow, self).__init__(parent = parent)

        self.setupUi(self)
        self.threadclass = ThreadClass()
        self.threadclass.start()
        self.threadclass.battery.connect(self.updateLevels)

    def updateLevels(self, val):
        self.battery1bar.setValue(val)    

class ThreadClass(QtCore.QThread):
    battery = pyqtSignal(int)
    def __init__(self, parent = None):
        super(ThreadClass, self).__init__(parent = parent)

        self.getLevels()

    def getLevels(self):
        self.battery=7
        self.battery.emit()

gs.Ui_MainWindow.setupUi is an auto generated ui from qt designer. getLevels will eventually dynamically retrieve battery levels.


Solution

  • You are hiding the signal since you are assigning it a value:

    self.battery = 7
    

    What you must do is pass it that value as a parameter in the emit():

    def getLevels(self):
        self.battery.emit(7)