This is my first attempt at trying to subclass QThreads and use it in a program but I'm getting something kind of weird. I'm not sure if my constructor is wrong or something like that, but basically when I run my QThread the entire program sleeps (not just the thread) when the QThread is sleeping
For example, the provided code will print "Hello there" after 3 seconds which is how long the QThread is supposed to sleep for
How do I fix my code so that I can have my thread running in the background while the program is running
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QThread, pyqtSignal
import time
class MyThread(QThread):
def __init__(self):
QThread.__init__(self)
def __del__(self):
self.wait()
def run(self):
self.sleep(3)
print("Slept for 3 seconds")
def main():
qThread = MyThread()
qThread.run()
print("Hello there")
main()
Use start
not run
:
def main():
qThread = MyThread()
qThread.start()
print("Hello there")
Since run
is the starting point for the thread (which exists in case you wanted to reuse the code not in a thread),
whilst start
is the method to start the thread itself, so it will in turn call run