[PyKDE] qt.qApp, subclassing QApplication
Phil Thompson
phil at riverbankcomputing.co.uk
Sat Jan 17 18:11:01 GMT 2004
On Saturday 17 January 2004 4:43 pm, dan at tombstonezero.net wrote:
> On Thu, 15 Jan 2004 19:15:06 +0000,
>
> Phil Thompson <phil at riverbankcomputing.co.uk> wrote:
> > On Thursday 15 January 2004 10:58 am, axel.mittendorf at transfertech.de
wrote:
> > > 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 have this same issue. In specific answer to your question,
> here's an excerpt from my program:
>
> class MyApp( QApplication ):
>
> def __init__( self, argv ):
> QApplication.__init__( self, argv )
>
> if __name__ == "__main__":
>
> app = MyApp( sys.argv )
> w = MyWidget( )
> app.setMainWidget( w )
> w.show( )
> print 'main:', app
> print 'main:', qApp
> app.exec_loop( )
>
> and its output:
>
> main: <__main__.MyApp object at 0xf2690>
> main: <__main__.qt.QApplication object at 0xf2330>
>
> qApp does *not* refer to the instance of my subclass.
Sorry, I wasn't clear. They refer to the same C++ instance.
> > > 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
>
> Where is the right place to do this? Inside MyApp.__init__?
> Inside main, after I create app?
As early as possible, which would be in the __init__() method. Note, however,
that any module that has already imported qt won't see it unless you also
update that module's dictionary.
> Is this still valid/safe in light of my example above?
Yes.
> > 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.
>
> That is exactly the case: that IWBNI the type of qApp were my
> subclass rather than qApplication. Perhaps this is a design flaw,
> but I decided to add some application-wide utility functions to my
> application subclass, mostly thin wrappers around "emit( PYSIGNAL(
> 'somesignal' ), *args )" sorts of utility functions. That way, I
> can reference them from anywhere as qApp.utilityfunction.
I think it's better to use a separate myApp global that you set in
MyApp.__init__(). Otherwise you are just making your code more difficult to
understand.
Phil
More information about the PyQt
mailing list