[PyQt] super()
Phil Thompson
phil at riverbankcomputing.co.uk
Thu Jul 19 08:16:23 BST 2007
On Wednesday 18 July 2007 10:32 pm, Carlos Scheidegger wrote:
> Hello,
>
> Is there a recommended way of using super() on PyQt objects? Is that
> considered in general a horrible idea? Here's the problem. We have a few
> mixin classes we use in our code, and we multiply inherit them. Some of
> these mixins change the event handling behavior. Here's a simple example:
>
> class Mixin(object):
>
> def moveEvent(self, event):
> do_something_here()
> super(Mixin, self).moveEvent(event)
>
> class SomeBaseWidget(QtGui.QWidget, Mixin):
>
> ...
>
> class SomeDerivedWidget(SomeBaseWidget):
>
> def moveEvent(self, event):
> do_yet_another_thing()
>
> # This calls Mixin, and Mixin calls QtGui.QWidget.moveEvent
> super(SomeDerivedWidget, self).moveEvent(event)
>
> This particular example works fine. For uniformity, however, I would like
> to use super() in all call sites that pass the event to the parent class.
> However, this fails with "AttributeError: 'super' object has no attribute
> 'moveEvent'.":
>
> class VanillaWidget(QtGui.QWidget):
>
> def moveEvent(self, event):
> super(VanillaWidget, self).moveEvent(event)
>
> it seems the proxy object returned by subject is not dispatching the call
> to where I expected it to. This is easy to exercise with the following
> patches to t5.py. The first patch works fine:
>
> +++ t5.py 2007-07-18 15:23:45.000000000 -0600
> @@ -30,6 +30,10 @@
> layout.addWidget(lcd);
> layout.addWidget(slider);
> self.setLayout(layout);
> +
> + def moveEvent(self, event):
> + print "Called"
> + QtGui.QWidget.moveEvent(self, event)
>
> app = QtGui.QApplication(sys.argv)
> widget = MyWidget()
>
> The second patch raises errors:
>
> +++ t5_bad.py 2007-07-18 15:24:25.000000000 -0600
> @@ -30,6 +30,10 @@
> layout.addWidget(lcd);
> layout.addWidget(slider);
> self.setLayout(layout);
> +
> + def moveEvent(self, event):
> + print "Called"
> + super(MyWidget, self).moveEvent(event)
>
> app = QtGui.QApplication(sys.argv)
> widget = MyWidget()
>
>
> I would like all the function calls to be consistent so that there's less
> risks of future gotchas when changing classes to use mixins. Is multiple
> inheritance in PyQt generally considered a bad idea? What are PyQt best
> practices when it comes to mixins?
http://www.riverbankcomputing.com/Docs/PyQt4/pyqt4ref.html#super-and-pyqt-classes
Phil
More information about the PyQt
mailing list