Overloading virtual method of widget without inheriting (PyQt)

A.T.Hofkamp hat at se-162.se.wtb.tue.nl
Tue May 27 04:55:24 EDT 2008


On 2008-05-27, Alex Gusarov <alex.m.gusarov at gmail.com> wrote:
> Hello, I have strong .NET background with C# and want to do some
> familiar things from it with Python, but don't know how. For example,
> I created form in qt designer with QCalendarWidget, translated it into
> Python module and want to overload virtual method paintCell of
> QCalendarWidget. In C# I can write following (abstract) code:
>
> this.calendar.PaintCell += new PaintEventHandler(myPaintCellHandler);
>
> void myPaintCellHandler(object sender, PaintEventArgs e) {
>     // some work here
> }

Not sure what you are doing here precisely, as I have no knowledge at all in
C#. It may be useful to explain in words what you intend to do.

> I can't find how I can do similar thing in Python without inheriting
> QCalendarWidget and overloading this method in inherited class (it's
> long and I must create additional class). The only thing I done its

It is going to be long in both cases, assuming you code the same functionality.
Why are you so worried about 5 lines of code (for making a new class)?


> full replacement of handler:
>
> calendar.paintCell = myPaintCell
>
> def myPaintCell(self):
>     pass
>
> Operator += don't work with methods. So, I can't add handler or call
> standart handler from my (infinite recursion, cause my handler
> replaced standart)

Yes you can, prefix with the class name, as in

def myPaintCell(self):
    PaintCell.paintcell(self) # Call base class
    pass

> Please, give me some advice, I know Python must be good enough to do
> such things fast and elegant.

In my view, 'elegant' would be to derive a new class:

class MyPaintCell(PaintCell):
    def paintcell(self):
        PaintCell.paintcell(self)
        myPaintCell(self)

(hmm, only 4 lines, I overestimated the cost. Sorry)

Sincerely,
Albert



More information about the Python-list mailing list