call function of main thread from secondary thread

自古美人都是妖i 提交于 2019-12-02 05:47:34

While searching for answer, qtcentre helped with it.

  1. You need to have seperate class for signals
class MySignal(QtCore.QObject):
    sig = QtCore.pyqtSignal(list)
    sigStr = QtCore.pyqtSignal(str)

This signal is used to communicate between threads.

  1. to communicate from main thread to worker thread,

    create instance of qthread in init of class where Ui is defined to pass parameters from main thread either in init or where required.

class MyThread(QtCore.QThread):

  def __init__(self, parent = None, *args, **kw):             .           .
      self.setData(*args, **kw)


  def setData(self, userShotList, inData, outData, dept):
      self.userShotList = userShotList            .           .

This way data is passed from main to worker.

  1. to communicate from worker thread to main thread

class MyThread(QtCore.QThread):

  def __init__(self, parent = None, *args, **kw):             .           .           .           .
      self.signal = MySignal()

where required execute signal with different types ( list, str ...) defined in MySignal()

def xyz(self): self.signal.sigStr.emit(message)

Hope this helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!