Results 1 to 2 of 2

Thread: pyqt: Start new thread and keep GUI reponsive whilst this new thread is still running

  1. #1
    Join Date
    Aug 2015
    Qt products

    Default pyqt: Start new thread and keep GUI reponsive whilst this new thread is still running

    Hi all,

    There a problem I've been wrestling with when working in PyQt4 and it should be fairly simple... but I find it isn't.

    I want to perform a calculation on another thread and have the GUI wait for the calculation to finish but remain responsive whilst waiting. By "responsive" I don't mean the user should be doing anything, I just don't want the GUI to freeze.

    I have something like:

    class worker(QObject):
    def __init__(self, args):
    super(worker, self).__init__()

    print QThread.currentThreadId()

    self.args = args

    start = pyqtSignal()

    def __del__(self):

    def run(self):

    print QThread.currentThreadId()
    '''do a long calculation using self.args'''

    myworker = worker(args)

    mythread = QtCore.QThread()
    print mythread.currentThreadId()

    print mythread.currentThreadId()

    print mythread.currentThreadId()

    // keep the GUI responsive until the long calc on another thread has finished
    while mythread.isRunning():

    You will notice that I print the thread Id in several places. The output tells me that only the run method is running on a different thread and so the while loop is an inifinite loop because mythread is running on the GUI thread, not the worker thread that run is running on. How can I keep running QtGui.QApplication.processEvents() until run has finished executing?

    Any help with this would be greatly appreciated. Many thanks in advance

  2. #2
    Join Date
    Jan 2006
    Graz, Austria
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Unix/X11 Windows

    Default Re: pyqt: Start new thread and keep GUI reponsive whilst this new thread is still run

    currentThreadId() is a static method, it prints the ID of the thread executing it.
    So whenever you call it from the main thread, it evaluates to the main thread's ID.


Similar Threads

  1. Replies: 4
    Last Post: 17th October 2013, 11:12
  2. Start a thread automatic at start
    By ralphot in forum Qt Programming
    Replies: 3
    Last Post: 10th July 2013, 15:54
  3. thread->start() does not work ?
    By ErrMania in forum Newbie
    Replies: 3
    Last Post: 20th November 2011, 11:08
  4. Segmentation fault whilst running QtLocation app
    By Ereb in forum Qt Programming
    Replies: 7
    Last Post: 9th December 2010, 10:32
  5. How to start a thread (inline)
    By DiamonDogX in forum Qt Programming
    Replies: 4
    Last Post: 28th May 2009, 21:53

Tags for this Thread


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.