Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. Extreme confusion over Qthread and avoiding subclassing
Qt 6.11 is out! See what's new in the release blog

Extreme confusion over Qthread and avoiding subclassing

Scheduled Pinned Locked Moved Unsolved Language Bindings
2 Posts 2 Posters 890 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jgv115
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved