How to make Qt work when main thread is busy?

Viewed 28558

Main (function main is there) thread of my program is reserved for non-GUI tasks. It calls a number of lengthy calculation functions. All implemented GUI's have been doing their work in a separate threads.

I'm now going to implement one more GUI using Qt. Qt documentation says all GUI related tasks should be done in main thread. In my case, inserting occasional QCoreApplication::processEvents() calls in main thread would be virtually useless due to great delays between them.

Is there any way to overcome this constraint of Qt? Is it impossible to do something non-GUI related in main thread of Qt program?

4 Answers

The concept of main thread is not clearly defined in Qt documentation. Actually, the main thread of a process (process that executes the Process.run function) can be different from the main Qt thread (thread that instantiates the first Qt object like a QApplication), although both "main" threads are often the same one.

Example of valid code structure:

function below will run in the process' non-main thread 'thread-1', that will become immediately Qt's main thread.

def startThread1():      
    app = QApplication(sys.argv)
    app.exec_()  # enter event loop

code below run in process' main thread, not to be confused with the main Qt and unique GUI thread of the process.

thread1 = Thread(target=self.startThread1)
thread1.start()
input('I am busy until you press enter')
Related