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
    Posts
    3
    Qt products
    Platforms
    Unix/X11

    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):
    pass

    @pyqtSlot()
    def run(self):

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




    myworker = worker(args)

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

    mypcaworker.moveToThread(mythread)
    mythread.started.connect(mypcaworker.run)
    print mythread.currentThreadId()

    mythread.start()
    print mythread.currentThreadId()

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




    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
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    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.

    Cheers,
    _

Similar Threads

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

Tags for this Thread

Bookmarks

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.