[PyKDE] Re: How can I prevent a QLable from eating MouseEvents?

Markus Gritsch gritsch at iue.tuwien.ac.at
Sat May 26 14:52:51 BST 2001


Hi!

I have found a solution by installing an EventFilter:


import sys
from qt import *


class MyEventFilter(QObject):
    def __init__(self, parent):
        QObject.__init__(self, parent)
        self.parent = parent

    def eventFilter(self, object, event):
        if event.type() == QEvent.MouseButtonPress:
            print 'click'
            return 1
        return 0


class Form1(QDialog):
    def __init__(self, parent=None, name='Form1', modal=0, fl = 0):
        QDialog.__init__(self,parent,name,modal,fl)

        self.resize(120,60)
        self.setCaption(self.tr('Form1'))

        self.TextLabel1 = QLabel(self, 'TextLabel1')
        self.TextLabel1.setGeometry(QRect(30,20,61,15))
        self.TextLabel1.setText('TextLabel1')

        self.myeventfilter = MyEventFilter(self)
        self.installEventFilter(self.myeventfilter)
        self.TextLabel1.installEventFilter(self.myeventfilter)


a = QApplication(sys.argv)
QObject.connect(a,SIGNAL('lastWindowClosed()'),a,SLOT('quit()'))
w = Form1()
a.setMainWidget(w)
w.show()
a.exec_loop()


> How can I prevent a QLable from eating MouseEvents?  If I click
> outside the lable in the following example, mousePressEvent() is
> called, if I click on the lable it is not.  What can I do to receive
> MouseEvents in any case?
> 
> 
> import sys
> from qt import *
> 
> class Form1(QDialog):
>     def __init__(self, parent=None, name='Form1', modal=0, fl = 0):
>         QDialog.__init__(self,parent,name,modal,fl)
> 
>         self.resize(120,60)
>         self.setCaption(self.tr('Form1'))
> 
>         self.TextLabel1 = QLabel(self, 'TextLabel1')
>         self.TextLabel1.setGeometry(QRect(30,20,61,15))
>         self.TextLabel1.setText('TextLabel1')
> 
>     def mousePressEvent(self, mouseEvent):
>         print 'click'
> 
> if __name__ == '__main__':
>     a = QApplication(sys.argv)
>     QObject.connect(a,SIGNAL('lastWindowClosed()'),a,SLOT('quit()'))
>     w = Form1()
>     a.setMainWidget(w)
>     w.show()
>     a.exec_loop()






More information about the PyQt mailing list