[PyKDE] qt.qApp, subclassing QApplication

Phil Thompson phil at riverbankcomputing.co.uk
Thu Jan 15 20:16:00 GMT 2004

On Thursday 15 January 2004 10:58 am, axel.mittendorf at transfertech.de wrote:
> Hi,
> in my application I want to subclass qt.QApplication and
> use this subclass instead of QApplication. Some of my
> modules are automatically generated by pyuic and I am
> not allowed to change their source code. The problem
> is these modules do "from qt import *" and use an object
> called "qApp" which seems to be an instance of qt.Application
> and I want them to use my subclass (exactly its instance)
> instead of "qApp". How can I solve this?

What makes you think that qApp doesn't refer to the instance of your 
QApplication sub-class?

> I wondered to overwrite qApp with my instance, does this
> work? Or will "qApp" be overwritten when a module does
> "from qt import *"?

You can overwrite it, but you have to make sure you do it in the right place. 
Something like...

import sys
import qt
sys.modules['qt'].__dict__['qApp'] = your_instance

However, qApp should already refer to the same C++ instance that was created 
when your sub-class instance was created. The only issue you might have is if 
you need the Python type of qApp to be the that of your sub-class instead of 
QApplication - in which case you will have to overwrite it.

> Can you tell me what qt.App is and what it is used for?
> I heared it was an instance of QApplication and I heared
> there can be only one QApplication instance in an application,
> but when I compare my own instance (which I have created in
> my code) with qApp ( "app is qApp") the result is false.
> So, are "app" and "qApp" two independent instances of
> QApplication? Could there be more than one?

qApp, in Qt, is a pointer to the (single) QApplication instance. It is 
initialised by each QApplication ctor. It doesn't matter if that ctor is 
called explicitly (as in most cases) or by a sub-class ctor (as in your 


More information about the PyQt mailing list