[PyKDE] Exceptions in callbacks
Bruce Sass
bsass at freenet.edmonton.ab.ca
Sat Feb 3 03:52:19 GMT 2001
Hi,
On 2 Feb 2001, Pete Ware wrote:
> I'm still wondering how to catch an exception that occurs in a slot.
> Supposed I wanted the following exception to print in a dialog box
> instead of to the console. How?
>
> Thanks,
> --pete
> ----------------------------------------------------------------------
> #! /bin/env python
> # -*- Python -*-
> import sys
> import qt
>
> class Test (qt.QFrame):
> def __init__ (self, parent = None, name = ''):
> qt.QFrame.__init__ (self, parent, name)
> top = qt.QPushButton ('Exception', self)
> top.setMinimumSize (top.sizeHint ())
> top.connect (top, qt.SIGNAL ('clicked()'), self.raise_exception)
>
> def raise_exception (self):
> raise 'An exception. How can I catch this?'
Hmmm, I'm not sure what you're getting at. All the program does is
raise an exception when a button is pressed... you're not raising the
exception within the context of anything so you can't trap it before
it causes Qt to abort. (I'm going from memory 'cause I don't have
PyQt installed right now)
> w = None
> def test ():
> global w
> app = qt.QApplication (sys.argv)
>
> w = Test (None, 'Table')
> app.setMainWidget (w)
> w.show ()
>
> return app
>
> if __name__ == '__main__':
> print 'starting app...',
> app = test ()
> print 'done'
To catch it before it gets to the native os, you should...
try:
> app.exec_loop ()
except:
# handle your exception
...but I don't think it is what you want.
how about...
def raise_exception(self):
"""'Exception' button pressed, handle it"""
try:
# whatever may cause an exception
except:
# what to do if there was an exception
else:
# what to do if there was no exception
You should check out the PyQt examples, maybe even compare them to the
Qt tutorials.
HTH
- Bruce
More information about the PyQt
mailing list