PyQt app in seperate thread

Phil Thompson phil at riverbankcomputing.co.uk
Wed Nov 22 14:14:02 CET 2006


On Wednesday 22 November 2006 12:37 pm, anders wrote:
> I am writing a plugin for a piece of software in python, and I want to
> start up a PyQt GUI in the plugin, without stalling the main thread
> while the gui is running (later i will want to pass messages between
> the main thread and the gui thread).
>
> I'm new to pyqt, so I'm probably doing something very silly, but for
> the moment I'm just trying to get the first pyqt tutorial example
> running in a seperate thread:
>
> ----8<--------
>
> import sys
> from PyQt4 import QtGui
> from PyQt4 import QtCore
>
> class MyThread( QtCore.QThread ):
> 	def __init__( self ):
> 		QtCore.QThread.__init__( self )
>
> 	def run( self ):
> 		app = QtGui.QApplication( sys.argv )
> 		hello = QtGui.QPushButton( 'Hello world!' )
> 		hello.resize( 500, 500 )
> 		hello.show()
> 		app.exec_()
> 		QtCore.QThread.terminate(  )
>
>
>
> mt = MyThread()
> mt.start()
> print 'Main thread continuing...'
> mt.wait()
> print 'GUI thread finished.'
>
> ----8<--------
>
> The app starts up (with a warning WARNING: QApplication was not created
> in the main() thread. ). I get a window, but no button, and clicking on
> the close button does nothing and I have to force the program to quit.
>
> There's got to be a way to get this working, right? Can anyone help me
> along the right path?

Read http://doc.trolltech.com/4.2/threads.html

In particular the bit that says that exec_() must be called from the main 
thread and not from a QThread.

Phil



More information about the Python-list mailing list