Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Unsolved PySide2 & Threading... how to set data on widget from thread?

    General and Desktop
    pyside2 thread
    2
    5
    1476
    Loading More Posts
    • 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.
    • D
      Dariusz last edited by

      Hey

      So this is interesting one... in C++ I would just roll with QMetaObject::InvokeMethod(widgetobject,=,t=data{widgetobject.setText(t)},Qt::QueuedConnection);.... but in PySide2... how do I do this ? Or whats the proper way ?

      My "other" thread is a different library and runs using native threading.Thread from python and not Qt one...

      Hints?

      TIA

      eyllanesc 1 Reply Last reply Reply Quote 0
      • eyllanesc
        eyllanesc @Dariusz last edited by

        @Dariusz Use signals for send information

        If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

        1 Reply Last reply Reply Quote 1
        • D
          Dariusz last edited by

          @eyllanesc said in PySide2 & Threading... how to set data on widget from thread?:

          @Dariusz Use signals for send information

          Yes I have a feeling thats what I need but what is the proper way of doing it ?

          class manager(QObject)
              sendData = Signal(object, str)
          
              def __init__():
                  self.sendData.connect(self.applyData)
          
              ### Execute on MAIN Thread
              def applyData(self, object, data):
                  object.setText(data)
          
              ### call from thread XXXX
              def sendIt(self, someWidget,data):
                  self.sendData.emit(someWidget, data)
          
          Like this? How do I know if he uses QueuedConnection or DirectConnection ? 
          
          TIA
          1 Reply Last reply Reply Quote 0
          • eyllanesc
            eyllanesc last edited by eyllanesc

            @Dariusz said in PySide2 & Threading... how to set data on widget from thread?:

            1. Do not implement the same logic in the same class so "manager" should only process the data and send it, then connect that signal to the GUI so that it updates the information.
            Class Worker(QObject):
                sendData = Signal(str)
                def process(self):
                    value = "foo"
                   self.sendData.emit(value)
            
            class GUI(QWidget):
                def apply_data(self, text):
                   self.foo.setText(text)
            
            worker = Worker()
            thread = QThread()
            worker.moveToThread(thread)
            
            gui = GUI()
            worker.sendData.connect(gui.apply_data)
            
            QTimer.singleShot(0, worker.process)
            
            1. By default in the connection AutoConnection is used so since the sender lives in the second thread and the receiver in the main thread then QueuedConnection will be used.

            If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

            D 1 Reply Last reply Reply Quote 1
            • D
              Dariusz @eyllanesc last edited by

              @eyllanesc said in PySide2 & Threading... how to set data on widget from thread?:

              @Dariusz said in PySide2 & Threading... how to set data on widget from thread?:

              1. Do not implement the same logic in the same class so "manager" should only process the data and send it, then connect that signal to the GUI so that it updates the information.
              Class Worker(QObject):
                  sendData = Signal(str)
                  def process(self):
                      value = "foo"
                     self.sendData.emit(value)
              
              class GUI(QWidget):
                  def apply_data(self, text):
                     self.foo.setText(text)
              
              worker = Worker()
              thread = QThread()
              worker.moveToThread(thread)
              
              gui = GUI()
              worker.sendData.connect(gui.apply_data)
              
              QTimer.singleShot(0, worker.process)
              
              1. By default in the connection AutoConnection is used so since the sender lives in the second thread and the receiver in the main thread then QueuedConnection will be used.

              Hmmm thank you, but will this work with python Thread ? As the worker thread, I got is not Qt...

              1 Reply Last reply Reply Quote 0
              • First post
                Last post