4

I am trying to add a PyQt GUI console in my already established application. But the PyQt GUI blocks the whole application making it unable to do rest of the work. I tried using QThread, but that is called from the mainWindow class. What I want is to run the MainWindow app in separate thread.

def main()
      app = QtGui.QApplication(sys.argv)
      ex = Start_GUI()
      app.exec_()  #<---------- code blocks over here !

      #After running the GUI, continue the rest of the application task
      doThis = do_Thread("doThis")
      doThis.start()
      doThat = do_Thread("doThat")
      doThat.start()

My application already uses Python Threads, So my question is, what is the best approach to achieve this process in a threaded form.

Anum Sheraz
  • 2,383
  • 1
  • 29
  • 54

1 Answers1

5

One way of doing this is

import threading

def main()
      app = QtGui.QApplication(sys.argv)
      ex = Start_GUI()
      app.exec_()  #<---------- code blocks over here !

#After running the GUI, continue the rest of the application task

t = threading.Thread(target=main)
t.daemon = True
t.start()

doThis = do_Thread("doThis")
doThis.start()
doThat = do_Thread("doThat")
doThat.start()

this will thread your main application to begin with, and let you carry on with all the other stuff you want to do after in the code below.

Pax Vobiscum
  • 2,551
  • 2
  • 21
  • 32
  • Thanks @UzzeeThat did the trick. Can you explain why is it necessary to set daemon = True ? .The code still runs smoothly by commenting out that line too. – Anum Sheraz Jun 08 '16 at 05:57
  • It means that it will be sort of a subprocess. I don't know the terminology exactly, but it means that the process will end when the task finishes. – Pax Vobiscum Jun 08 '16 at 05:58
  • 14
    Using any Qt GUI code in the non-main thread is unsupported and can lead to all kind of funny crashes. See [the Qt docs](http://doc.qt.io/qt-5/thread-basics.html#gui-thread-and-worker-thread). – The Compiler Jun 08 '16 at 07:08