[PyKDE] qt.qApp, subclassing QApplication

dan at tombstonezero.net dan at tombstonezero.net
Sat Jan 17 17:45:00 GMT 2004


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.

> > 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?

Is this still valid/safe in light of my example above?

> 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.

Regards,
Dan

-- 
I hear, and I forget.                                  Dan Sommers
I see, and I remember.                 [insert your own joke here]
I do, and I understand.             <mailto:dan at tombstonezero.net>
            -- Confucius       <http://www.tombstonezero.net/dan/>




More information about the PyQt mailing list