[PyQt] Re: How to override mousePressEvent for an existing QPushButton widget

Hans-Peter Jansen hpj at urpla.net
Thu Jul 30 10:32:52 BST 2009


Am Donnerstag, 30. Juli 2009 schrieb Dov Grobgeld:
> I wrote:
> > [stuff deleted]
> >
> >
> > This is actually related to another even more fundamental question, why
> > is the difference between overriding and connecting? I.e. why is
> > "clicked" a signal and "mousePressEvent" an overrided virtual. Why
> > can't they both be signals, in which case the problem I describe
> > wouldn't happen? (That's how it works in Gtk, btw.)
>
> I realized that the wording above is both unfortunate and wrong. I
> understand that there is a need for virtual functions, and Gtk indeed
> provides them as well. But what is less clear to me is why there is a
> distinction between "clicked" and "mousePressEvent", and why they are
> implemented differently? Anyhow, that has nothing to do with the Py part
> of PyQt which simply reflects the underlying API.

Because, conceptually, the clicked signal is generated in the 
mousePressEvent, thus the events are low level notifications, and signals 
the standard "high level" way of using this functionality (from apps 
perspective). 

Since there is no rightClicked signal available, you will want to look into 
QWidget.eventFilter() et.al. 

A cleaner approach would be to subclass a button widget, override 
mousePressEvent and generate the missing signal there. It's quite easy to 
add simple widgets to designer (nowadays). 

Note, the average user does not expect right click functionality from 
buttons, this is why such a signal does not exist in the first place. Thus 
your users might never discover it..

Pete


More information about the PyQt mailing list