473,387 Members | 1,579 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Overloading virtual method of widget without inheriting (PyQt)

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
7 3210
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Terje Slettebų | last post by:
To round off my trilogy of "why"'s about PHP... :) If this subject have been discussed before, I'd appreciate a pointer to it. I again haven't found it in a search of the PHP groups. The PHP...
2
by: Srinath Avadhanula | last post by:
Hello, Sorry to be bringing up what seems to be a somewhat beaten up topic... This is what I wanted to do: Create a _simple_ text editor widget which supports VI(M) style keybindings but...
10
by: Peter | last post by:
I want to draw some lines on a widget. This works ok, but when I want to redraw, the old lines are still there. How do I clear or refresh the widget, so I can draw a new set of lines? Code...
5
by: Torsten Curdt | last post by:
Let's assume I have a base class class X { }; and the the following classes inheriting class BX : public X {
3
by: Steven T. Hatton | last post by:
I stumbled upon this blog while googling for something. I have to say, I really don't understand what Lippman is trying to tell me here. I included the first paragraph for context, but the second...
31
by: | last post by:
Hi, Why can I not overload on just the return type? Say for example. public int blah(int x) { }
5
by: toton | last post by:
Hi, I want a few of my class to overload from a base class, where the base class contains common functionality. This is to avoid repetition of code, and may be reducing amount of code in binary,...
2
by: skawaii | last post by:
Ok, here's what's going on. I've just created a custom widget. it works great. I'm having some trouble, however, figuring out how to allow the said widget to resize. For example, when I throw the...
0
by: David Boddie | last post by:
On Mon May 26 17:37:04 CEST 2008, Alex Gusarov wrote: Right. I vaguely remember someone showing something like this at EuroPython a couple of years ago. I believe that this approach is actually...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.