⚠️ Forum Maintenance: Feb 6th, 8am - 14pm (UTC+2)
Extreme confusion over Qthread and avoiding subclassing
jgv115 last edited by
(I'm coding in python but the principles are the same)
So I'm developing a simple interface that reads a few values from an external device and pushes them to a few QLabels.
I did some research on QThreads and it seems like at the moment, I'm defining a Qthread in another class and executing code from the run method:
class worker(QThread): def __init__(self): Qthread.__init__(self) def run(self): do_work() a = 3 def do_work(self): b = 2 self.emit(SIGNAL("work_done"),b) class MyApp(QtGui.QMainWindow): QtGui.QMainWindow.__init__(self, parent) self.ui = Ui_MainWindow() self.ui.setupUi(self) self.worker_thread = worker() self.connect(self.worker,SIGNAL('work_done'),self.done) self.worker_thread.start() def done(self,b) print "the value from the worker is: " + str(b)
So I just have a few questions.
Does everything I execute in the run() method in my worker class execute in a different thread?
- I don't understand why it would not be as my UI is constantly updating while I do many many other calculations in the background
If it is bad practice, what are the potential pitfalls of this method
If I was to discard this code and redo it without subclassing the QThread, what is the proper way to do it?
Hi and welcome to devnet,
do_workwill be called in your thread.
For such a use case, it's a bit overkill, QtConcurrent::run would likely be simpler and cleaner.