473,287 Members | 1,708 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,287 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 3200
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.