473,795 Members | 2,839 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.P aintCell += new PaintEventHandl er(myPaintCellH andler);

void myPaintCellHand ler(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.paintC ell = myPaintCell

def myPaintCell(sel f):
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 3229
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.P aintCell += new PaintEventHandl er(myPaintCellH andler);

void myPaintCellHand ler(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.paintC ell = myPaintCell

def myPaintCell(sel f):
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(sel f):
PaintCell.paint cell(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(Pai ntCell):
def paintcell(self) :
PaintCell.paint cell(self)
myPaintCell(sel f)

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

Sincerely,
Albert
Jun 27 '08 #2
class MyPaintCell(Pai ntCell):
def paintcell(self) :
PaintCell.paint cell(self)
myPaintCell(sel f)

(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(Pai ntCell):
def paintcell(self) :
PaintCell.paint cell(self)
myPaintCell(sel f)

(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(ob ject):
def __init__(self, owner, *initial_handle rs):
self.owner = owner
self.handlers = list(initial_ha ndlers)
def __iadd__(self, handler):
self.handlers.a ppend(handler)
return self
def __call__(self, *args, **kwargs):
for handler in self.handlers:
handler(self.ow ner, *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_pai nt = MultiHandler(ca lendar, Calendar.on_pai nt)

calendar.on_pai nt()
calendar.on_pai nt += handler1
calendar.on_pai nt()
calendar.on_pai nt += handler2
calendar.on_pai nt()
Jun 27 '08 #5
class MultiHandler(ob ject):
def __init__(self, owner, *initial_handle rs):
...
...
...
calendar = Calendar()
calendar.on_pai nt = MultiHandler(ca lendar, Calendar.on_pai nt)

calendar.on_pai nt()
calendar.on_pai nt += handler1
calendar.on_pai nt()
calendar.on_pai nt += handler2
calendar.on_pai nt()
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.P aintCell += new PaintEventHandl er(myPaintCellH andler);

void myPaintCellHand ler(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.paintC ell = myPaintCell

def myPaintCell(sel f):
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.paintC ell.signal( SOME_SIGNAL_NAM E, 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.paintC ell.signal( SOME_SIGNAL_NAM E, 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
4727
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 manual mentions "overloading" (http://no.php.net/manual/en/language.oop5.overloading.php), but it isn't really overloading at all... Not in the sense it's used in other languages supporting overloading (such as C++ and Java). As one of the...
2
3746
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 works with arbitrary fonts/unicode characters. Vi(m) unfortunately, does not support Devanagari (or proportional fonts) and it looks like it will take quite some time for these things to work. As
10
11719
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 snip below. TIA Peter
5
3008
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
1641
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 paragraph is the one that has me most confused. <quote url="http://blogs.msdn.com/slippman/archive/2004/01/27/63473.aspx"> In C++, a programmer can suppress a virtual call in two ways: directly use an object of the class, in which case the...
31
2294
by: | last post by:
Hi, Why can I not overload on just the return type? Say for example. public int blah(int x) { }
5
1991
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, not to get polymorphic behavior. None of them has virtual methods, and are self contained (no destructor at all) thus do not have a chance to have memory error. Thus the derived classes has additional functionality, not additional data.
2
3458
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 widget into a new dialog (using Qt Designer), I want to be able to drag it larger/smaller and have widget expand/shrink to that size. Right now, I can drag the widget bigger/smaller all I want, but it doesn't actually change size. I'm pretty...
0
1071
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 registering an event handler (or callback) to handle a certain type of event. Just out of interest, given that you have to put the event handler somewhere, why is it a problem to derive a new class and reimplement paintCell()?
0
10443
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10165
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10002
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9044
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6783
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5565
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2921
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.