Results 1 to 4 of 4

Thread: Waiting for QThread without interupting main thread

  1. #1
    Join Date
    Apr 2015
    Posts
    28
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Waiting for QThread without interupting main thread

    How do you wait for a qthread to finish without blocking the main loop (thereby blocking the responsiveness of the main window)? I'm using PyQt5.

    Qt Code:
    1. # When scan button is pressed
    2. def scan(self, e):
    3. for dev in self.deviceList:
    4. thread = TestThread(self, dev)
    5. thread.debugOutput.connect(self.updateTextEdit)
    6. thread.start()
    7. print("Thread finished")
    To copy to clipboard, switch view to plain text mode 

    The thread(s) started in this piece of code do GUI updates to inform the user of what's going on. I do not want to block that. I want the "thread finished" to be printed out once the thread actually is finished.

  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: Waiting for QThread without interupting main thread

    QThread has a finished() signal.

    Cheers,
    _

  3. #3
    Join Date
    Apr 2015
    Posts
    28
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Waiting for QThread without interupting main thread

    Like this?

    Qt Code:
    1. # When scan button is pressed
    2. def scan(self, e):
    3. for dev in self.deviceList:
    4. thread = TestThread(self, dev)
    5. thread.debugOutput.connect(self.updateTextEdit)
    6. thread.start()
    7. thread.finished.connect(self.isFinished)
    8.  
    9. def isFinished(self):
    10. print("Thread finished")
    To copy to clipboard, switch view to plain text mode 

    I thought I might post it here, it took me a while to figure it out (newbie section, right?)

  4. #4
    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: Waiting for QThread without interupting main thread

    Quote Originally Posted by ecce View Post
    Like this?
    Not quite.

    You need to connect before you call start, otherwise the thread could have started and finished before the caller thread had a chance to establish the connection.

    The most important principle of multithread programming is to never make assumptions of when something gets executed, unless such an assumption is enforced by means like synchronization or, like in this case, doing things before there is any concurrent execution.

    Cheers,
    _

Similar Threads

  1. Emit signal from QThread to main thread!!!
    By sanujas in forum Qt for Embedded and Mobile
    Replies: 2
    Last Post: 26th December 2013, 15:22
  2. Replies: 5
    Last Post: 22nd June 2012, 17:40
  3. Replies: 5
    Last Post: 22nd February 2011, 22:21
  4. QThread sends signal to main thread immediatly
    By BIllNo123 in forum Newbie
    Replies: 7
    Last Post: 27th August 2010, 11:32
  5. Determine if QThread::currentThread is main thread?
    By chaoticbob in forum Qt Programming
    Replies: 2
    Last Post: 17th December 2008, 07:26

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.