[PyQt] override className() in QMetaClass

Rafael Durán Castañeda rafadurancastaneda at gmail.com
Fri Sep 9 11:20:11 BST 2011


I think you can do something like this:

 >>> import PyQt4.QtGui as gui
 >>> def factory(name):
...     def spam(): pass
...     def yam(): pass
...     return type(name, (gui.QWidget,),{'spam':spam, 'yam': yam})
...
 >>> MyClass = factory('MyClass')
 >>> MyClass.__mro__
(<class '__main__.MyClass'>, <class 'PyQt4.QtGui.QWidget'>, <class 
'PyQt4.QtCore.QObject'>, <type 'sip.wrapper'>, <class 
'PyQt4.QtGui.QPaintDevice'>, <type 'sip.simplewrapper'>, <type 'object'>)

On 08/09/11 23:33, Daniel Goertzen wrote:
> Does pyqt provide a way to override the class name when deriving a 
> QObject based class?
>
> An example of why this is wanted:
>
> def my_widget_class_factory(...):
>    class temp(QWidget):
>       ...(dynamically generate class attributes and methods)
>    return temp
>
> All classes generated by the above function will come back with 
> className() as "temp".  This causes headaches for Designer.
>
> I know I can add a metaclass to mangle the class name, but I was 
> wondering there was an easier way (ie, does QObject's metaclass 
> provided a way to rename the class?)
>
> Thanks,
> Dan.
>
>
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20110909/bdf01108/attachment-0001.html>


More information about the PyQt mailing list