[PyQt] Singelton from QObject

Paul Giannaros ceruleanblaze at gmail.com
Wed May 23 09:09:15 BST 2007

On Wednesday 23 May 2007 05:53:58 Nahuel Defossé wrote:
> I saw in ActiveState page the folowing code to make a singleton:
> class Borg:
>     __shared_state = {}
>     def __init__(self):
>         self.__dict__ = self.__shared_state
>     # and whatever else you want in your class -- that's all!
> And in all Qt classes I customize via inheritance I do the folowing:
> class MyClass(QSomeName):
> 	def __init__(self):
> 		QSomeName.__init__(self)
> I want to make a config class that recives some signals, so I inhertit from
> QObject. I did the folowing in the constructor...
> def __init__(self):
>     if self.__shared_state == None:
>       QObject.__init__(self)
>     self.__dict__ = self.__shared_state
> It seems to work, but I'm not sure. Have you ever tried to do something
> like that?

That is not a singleton, it is a borg -- you can have many different instances 
of a borg class but they all share the same state. Hypothetically it's a nice 
distinction but in practice, I find it's not really worth it. 
For my Qt singletons I just subclass from a QObject-based class and make sure 
I parent to qApp (especially when using C++ to avoid memory leaks easily), 
then carry on with normal singleton stuff.

> Thanks
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt

More information about the PyQt mailing list