[PyQt] Newbie's question about inheritance in QtPy4 classes

Hans-Peter Jansen hpj at urpla.net
Wed Aug 18 21:11:13 BST 2010


On Wednesday 18 August 2010, 21:15:20 Igor pesando wrote:
> Hi all,
>
> sorry to bother but I'm learning PyQt4 and  designer.
> Playing with the code suggested in some tutorials I came with the code
> below which should NOT work according to my understanding of inheritance
> but it works. The problem is that the class MyForm inherits from
> QMainWindow which does not inherit from QDockWidget even if they both
> inherit from QWidget.
> Therefore QtGui.QDockWidget.__init__(self, parent)  should fail but
> everything works fine.
> As a matter of fact  I tried a similar construction within plain
> python and I get an error message.
> So the question is why QtGui.QDockWidget.__init__(self, parent) works?
>
>
>
> import sys
> from PyQt4 import QtCore, QtGui
>
>
> class MyForm(QtGui.QMainWindow):
>     def __init__(self, parent=None):
>         QtGui.QDockWidget.__init__(self, parent)
>
>
> if __name__ == "__main__":
>     app = QtGui.QApplication(sys.argv)
>     myapp = MyForm()
>     myapp.show()
>     sys.exit(app.exec_())

This works because both classes inherits QWidget, and you don't try to make 
any use from your form. Otherwise you would get you you deserve.. 

See it as a special case of freedom, that you better avoid ;-).

I can imagine, that it's going to be hairy to catch such faults for sip, aka 
Phil.. 

I-will-always-use-super-to-init-by-baseclasses-ly y'rs,
Pete


More information about the PyQt mailing list