Mailing List Archive

Events with pyKDE 0.8.
I have defined an event as follows.

def mousePressEvent(self,e):
if e.button()==ButtonState,RightButton:
self.menu()

Now. this works fine on the mainwidget. But how do I define it to work with
a widget placed on the main widget. Ex, a label.
I have tried to find an explanation in the Qt-manual but with out luck.
There are no examples as far as I can see that illustrates this.

Very greatefull for help

/Peter Torstensson
Events with pyKDE 0.8. [ In reply to ]
Peter Torstenson <p.t@iname.com> wrote:
> I have defined an event as follows.
> [..]
> Now. this works fine on the mainwidget. But how do I define it to work with
> a widget placed on the main widget. Ex, a label.
Look at the source below. You have got two possibilities to solve your problem.
Either you create you own label through subclassing QLabel and implementing
mousePresEvent or you can catch the events for a simple QLabel with
installEventFilter. The second method is quite cool because you can use it with
any widget without sub-classing (I'm working on a dialog-editor using this
feature). Unfortunately you cannot (as far as I known) convert the QEvent to
QMouseEvent in eventFilter. In C++ it is simple macro (definded in qevent.h)
but pyKDE does not support this (even not in pyKDE 0.9pre2 :-( ). This would
be necessary to access to mouse position.

Greets
Henning


--------------------------------------------------------------
from kde import *

class MyLabel(QLabel):
def mousePressEvent(self, event):
print "mouse button on %s pressed" % self

class Window(QDialog):
def __init__(self, *args):
apply(QDialog.__init__, (self,) + args)
self.label1 = QLabel("Hello", self)
self.label1.installEventFilter(self) # catch events
self.label2 = MyLabel("World!", self)
self.label2.move(self.label1.width(), 0)

def eventFilter(self, object, event):
if event.type() == Event_MouseButtonPress:
print "mouse button on %s pressed" % object
return 1 # event handled
else:
return 0 # pass event to next handler

import sys
app = QApplication(sys.argv)
win = Window()
app.setMainWidget(win)
app.mainWidget().show()
app.exec_loop()