PyQt, main window can't have reference to application class

Phil Thompson phil at riverbankcomputing.co.uk
Mon May 24 13:22:58 EDT 2004


On Monday 24 May 2004 3:54 pm, Sibylle Koczian wrote:
> Still trying to learn PyQt from a book about several Python GUI
> toolkits, I seem to learn first what doesn't work. The following small
> script seems to work, but after closing the window I get the error
> message "Fatal Python error: PyEval_RestoreThread: NULL tstate".

You have created a circular reference which won't help. The problem doesn't 
occur if PyQt is built with SIP v4 - it uses the newer Python thread API.

> Without the line "self.app = app" the error goes away. So I suppose the
> main window can't have a reference to the application class. Right?
> Using the global variable qApp instead of self.app doesn't work either:
> qApp seems to be an instance of QApplication, not an instance of HelloApp.

It's the same C++ instance, but they are different types. knopftext is an 
attribute of the HelloApp type. qApp is of type QApplication.

> Should the GUI independent actions of the application rather be methods
> of the main window? Or methods of some class not derived from QObject,
> with a reference to an instance of this class in the main window
> instance and not in the application instance? Comparing with Delphi I
> suppose this might be the better way. I'd like to use the same class
> with different GUI toolkits (Tkinter, wxPython), so all the GUI
> independent stuff should be separate.
>
> #!/usr/bin/python
> # -*- coding: latin-1 -*-
> # hello2.py
>
> import sys
> from qt import *
>
> class HelloWindow(QMainWindow):
>
>      def __init__(self, app, *args):
>          QMainWindow.__init__(self, *args)
>          self.app = app       #### this seems to be the problem ####
>          self.button = QPushButton(self.app.knopftext, self)
> 	# self.button = QPushButton(qApp.knopftext, self)   #### won't run at
> all ####
>          self.setCentralWidget(self.button)
>          self.connect(self.button, SIGNAL('clicked()'), self,
> SLOT('close()'))
>
> class HelloApp(QApplication):
>
>      def __init__(self, *args):
>          QApplication.__init__(self, *args)
> 	self.knopftext = 'HelloApp'
>          self.connect(self, SIGNAL('lastWindowClosed()'),
>                          self, SLOT('quit()'))
>          self.MainWindow = HelloWindow(self)
>          self.setMainWidget(self.MainWindow)
>          self.MainWindow.show()
>
> def main(args):
>      app = HelloApp(args)
>      app.exec_loop()
>
> if __name__ == '__main__':
>      main(sys.argv)
>
> Thank you,
> Koczian

Phil




More information about the Python-list mailing list