Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Not sure if I'm using QThread properly
Forum Update on Monday, May 27th 2025

Not sure if I'm using QThread properly

Scheduled Pinned Locked Moved Solved Qt for Python
5 Posts 2 Posters 493 Views
  • 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.
  • T Offline
    T Offline
    Tauri
    wrote on last edited by Tauri
    #1

    Hello,

    I'm using qml as my front end, and I connect it using signals and slots to the backend.
    I have some data that I'm processing after selecting a file and my ui is frozen until that process is complete which is why I wanted to try and solve that using QThread.

    This is a simplified version of how I'm currently using it and it's not working for me (ui still frozen):

    from functools import partial
    
    class Secondary(QObject):
        update_list = Signal()
    
        def __init__(self):
            QObject.__init__(self)
    
        def process_data(self, file):
            do stuff
            self.update_list.emit()
    
    
    class Main(QObject):
    
        def __init__(self):
            QObject.__init__(self)
            self.m_thread = QThread(self)
            self.m_thread.start()
            self.secondary = Secondary()
            self.secondary.moveToThread(self.m_thread)
            self.secondary.update_list.connect(method_to_update_list)
            
        @Slot()
        def load_file(self):
            wrapper = partial(self.secondary.process_data, file)
            QTimer.singleShot(0, wrapper)
    

    The "load_file" method is called from the ui, and then the ui is frozen until "process_data" in the Secondary class is complete.
    What am I missing? Or am I just not using it correctly at all?

    Thanks in advance

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

      Add a signal to your MainWindow class, connect it to your worker object slot and emit it when appropriate.

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

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

        Hi,

        I am guessing that the "partial" short circuits the Qt connection detection and thus the slot is called in the main thread context.

        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
        • T Offline
          T Offline
          Tauri
          wrote on last edited by Tauri
          #3

          Hi @SGaist,

          I tried replacing the partial with either:

          QTimer.singleShot(0, self.secondary, SLOT(self.secondary.process_data(file)))
          

          Which runs successfully but gives me the following:
          RuntimeWarning: MetaObjectBuilder::addMethod: Invalid method signature provided for ""
          QTimer.singleShot(0, self.secondary, SLOT(self.secondary.process_data(file)))
          qt.core.qobject.connect: QObject::connect: Parentheses expected, slot Secondary::

          Not sure exactly what it means or what I should do to avoid it.

          Or:

          QTimer.singleShot(0, lambda : self.secondary.process_data(file))
          

          Both work but the ui is still frozen just like when I was using partial, so I'm kinda lost as to why this is not working.

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

            Add a signal to your MainWindow class, connect it to your worker object slot and emit it when appropriate.

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

            T 1 Reply Last reply
            1
            • SGaistS SGaist

              Add a signal to your MainWindow class, connect it to your worker object slot and emit it when appropriate.

              T Offline
              T Offline
              Tauri
              wrote on last edited by
              #5

              @SGaist Thanks!
              I implemented Signal/Slot like you suggested and it's working great now without even needing QTimer.

              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