Extreme confusion over Qthread and avoiding subclassing



  • Hi everyone,

    (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.

    1. 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
    2. If it is bad practice, what are the potential pitfalls of this method

    3. If I was to discard this code and redo it without subclassing the QThread, what is the proper way to do it?

    Thanks all!


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Yes, do_work will be called in your thread.

    For such a use case, it's a bit overkill, QtConcurrent::run would likely be simpler and cleaner.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.