AttributeError: 'testclass' object has no attribute 'moveToThread'
-
Hello!
Ive been trying to run a class on a different thread, but its thowing an error and I cannot figure out why.
The error I get: AttributeError: 'testclass' object has no attribute 'moveToThread'
relevant code is below.My setup:
main.py contains the code posted below,
TelegramClass.py contains the code I want to run on a new thread.in the main.py file i have this code:
import TelegramClass ... class Ui_MainWindow(object): ... self.TelegramThread() def TelegramThread(self): self.telegramThread = QThread() self.telegramWorker = TelegramClass.testclass() self.telegramWorker.moveToThread(self.telegramThread) self.telegramThread.start()
in my TelegramClass.py:
class testclass(): def __init__(self): self.run() def run(self): print("ok")
what am I doing wrong?
Any help appreciated. -
testclass
needs to inherit from QObject. -
Thank you,
i changed it to this, and now it works:
class testclass(QObject): def __init__(self): QObject.__init__(self) self.run() def run(self): print("ok")
but now I am facing a new problem when I try to connect a signal:
if I add this before i call the start():
self.telegramThread = QThread() self.telegramWorker = TelegramClass.testclass() self.telegramWorker.moveToThread(self.telegramThread) self.tgSignal.connect(self.telegramWorker.run) self.telegramThread.start()
I get the error message:
TypeError: Ui_MainWindow cannot be converted to PyQt5.QtCore.QObject in this context
-
No idea, sorry.
-
@Igor86 said in AttributeError: 'testclass' object has no attribute 'moveToThread':
TypeError: Ui_MainWindow cannot be converted to PyQt5.QtCore.QObject in this context
What line of code causes this?
-
@jsulm said in AttributeError: 'testclass' object has no attribute 'moveToThread':
@Igor86 said in AttributeError: 'testclass' object has no attribute 'moveToThread':
TypeError: Ui_MainWindow cannot be converted to PyQt5.QtCore.QObject in this context
What line of code causes this?
self.tgSignal.connect(self.telegramWorker.run)
this one
-
class Ui_MainWindow(object):
self.tgSignal.connect(self.telegramWorker.run)
TypeError: Ui_MainWindow cannot be converted to PyQt5.QtCore.QObject in this contextIf
self
isUi_MainWindow
that too will need to inheritQObject
, you have it only fromobject
? Everything which emits a signal, and probably in practice anything which has a slot, needs to derive fromQObject
.