By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,965 Members | 1,690 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,965 IT Pros & Developers. It's quick & easy.

Overloading virtual method of widget without inheriting (PyQt)

P: n/a
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
}

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
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)

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

--
Best regards, Alex Gusarov
Jun 27 '08 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On 2008-05-27, Alex Gusarov <al************@gmail.comwrote:
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
Jun 27 '08 #2

P: n/a
class MyPaintCell(PaintCell):
def paintcell(self):
PaintCell.paintcell(self)
myPaintCell(self)

(hmm, only 4 lines, I overestimated the cost. Sorry)
Yeah, that's funny..

I don't want to do it 'cause I create my form within Designer and
simply drop Calendar widget to it. Then I need to translate it
into .py file, it's OK. But if I want to use a custom derived from
Calendar class instead of it, I need after every refreshing/
translating of form replace few lines in translated file.

I just want to know about existence of other method to do this in
class, that contain instance of Calendar.

By "this" I mean:
Instead of calling original method "paintcell" alone, call it with my
custom metod.

Anyway, thanks.
Jun 27 '08 #3

P: n/a
Alex Gusarov wrote:
> class MyPaintCell(PaintCell):
def paintcell(self):
PaintCell.paintcell(self)
myPaintCell(self)

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

Yeah, that's funny..

I don't want to do it 'cause I create my form within Designer and
simply drop Calendar widget to it. Then I need to translate it
into .py file, it's OK. But if I want to use a custom derived from
Calendar class instead of it, I need after every refreshing/
translating of form replace few lines in translated file.

I just want to know about existence of other method to do this in
class, that contain instance of Calendar.

By "this" I mean:
Instead of calling original method "paintcell" alone, call it with my
custom metod.
You should ask this on the PyQt-mailing-list. And you can try and experiment
with the module new & the function instancemethod in there.

Diez
Jun 27 '08 #4

P: n/a
Since the "+=" operator won't work on methods, you have to write your
own class that will simulate dispatching multiple handlers for an
event. Maybe something like the following? I don't know, though, if it
would cooperate with PyQt nicely.

class MultiHandler(object):
def __init__(self, owner, *initial_handlers):
self.owner = owner
self.handlers = list(initial_handlers)
def __iadd__(self, handler):
self.handlers.append(handler)
return self
def __call__(self, *args, **kwargs):
for handler in self.handlers:
handler(self.owner, *args, **kwargs)
return None
class Calendar(object):
def on_paint(self):
print 'i am the default handler'

def handler1(self):
print 'i am handler 1'

def handler2(self):
print 'i am handler 2'
calendar = Calendar()
calendar.on_paint = MultiHandler(calendar, Calendar.on_paint)

calendar.on_paint()
calendar.on_paint += handler1
calendar.on_paint()
calendar.on_paint += handler2
calendar.on_paint()
Jun 27 '08 #5

P: n/a
class MultiHandler(object):
def __init__(self, owner, *initial_handlers):
...
...
...
calendar = Calendar()
calendar.on_paint = MultiHandler(calendar, Calendar.on_paint)

calendar.on_paint()
calendar.on_paint += handler1
calendar.on_paint()
calendar.on_paint += handler2
calendar.on_paint()
Marek, this seems exactly what I want, thanks, I will try it.
Thanks everybody. Yes, I'm newbie, so may be it was a dumb question,
don't judge me.

--
Best regards, Alex Gusarov
Jun 27 '08 #6

P: n/a
On Tue, 27 May 2008 01:31:35 -0700, Alex Gusarov 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
}

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
full replacement of handler:

calendar.paintCell = myPaintCell

def myPaintCell(self):
pass
It is more a matter of the GUI toolkit you are using rather than the
language. In Python, they are many, but they are not as tighty integrated
with the language as in C#. Also, Python has a no standard support for
event handling, but again several non-standard library (e.g. twisted ) and
plus you can relatively easily cook your own recipe, has other posters
have shown you.

Anyway, IIRC (it's a long time since I used Qt), QT allows to connect
more than one slot with the same signal, so you should not need to
subclass or to create your own multi-dispatcher. Just doing:

calendar.paintCell.signal( SOME_SIGNAL_NAME, my_paint_method )

should work. I don't know which signal you should connect to, however.

This link gives you some detail on signal/slots in PyQT:

http://www.commandprompt.com/community/pyqt/x1408

Ciao
-----
FB
Jun 27 '08 #7

P: n/a
I have a feeling that the form produced by Qt Designer, once converted to
code, contains references to QCalendarWidget where you really want to use a
customized calendar widget. If so, you should "promote" the calendar widget
in Qt Designer to use your widget instead, and make sure you import the
module that supplies it in your application.
David, thanks for noticing about "promoting" within designer, it helped me.
Anyway, IIRC (it's a long time since I used Qt), QT allows to connect
more than one slot with the same signal, so you should not need to
subclass or to create your own multi-dispatcher. Just doing:

calendar.paintCell.signal( SOME_SIGNAL_NAME, my_paint_method )

should work. I don't know which signal you should connect to, however.

This link gives you some detail on signal/slots in PyQT:
Thanks, but actually, paintCell is not a signal, it's simply a virtual
method of caledarwidget.

--
Best regards, Alex Gusarov
Jun 27 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.