[PyQt] Multiple inheritance with item views

Andreas Pakulat apaku at gmx.de
Mon Feb 22 21:39:24 GMT 2010


On 22.02.10 21:09:28, Jugdish wrote:
> The reason I have BaseView inheriting from QAbstractItemView is because
> inside it I make calls like:
> 
> self.setItemDelegate(...)
> self.setDragEnabled(...)
> self.setEditTriggers(...)
> 
> and so on. These are all things that are common to both
> MyTreeView/MyTableView, and obviously wouldn't work if BaseView had no base
> class.

Sure they would, thats so nice about python being a dynamically typed
language - as long as self has that function during runtime it'll work.
And during runtime it does have that function because of subclassing
MyTableView from QTableView+BaseView (unless PyQt4 has a restriction).
Take this example:

class Bar:
    def my(self):
        self.goup()

class Foo:
    def goup(self):
        print "Hi", self

class FooBar( Foo, Bar ):
    def makeit(self):
        print "me:", self
        self.my()

f = FooBar()
f.makeit()

It'll just work.

> Any ideas of a better way to achieve this without duplicating code between
> the 2 views?

Avoiding code-duplication should only be done with subclassing if the
subclasses "make sense", i.e. if they'd also exist without wanting to
avoid the code-duplication. In other cases the duplicated code should
rather be put into some shared functions or the like.

Andreas

-- 
Your object is to save the world, while still leading a pleasant life.


More information about the PyQt mailing list