tags:

views:

352

answers:

1

I am having problem filtering the "mousePressEvent " with installEventFilter

MyTestxEdit is a widget that holds QTextEdit I want that all the events of QTextEdit will be handle by MyTestxEdit I have used the installEventFilter This Trick works well for events like keyPressEvent but doesn't handle the mousePressEvent what am i doing wrong?

import sys
from PyQt4.QtGui import QApplication, QErrorMessage
from KdeQt.KQApplication import KQApplication
from KdeQt.KQMainWindow import KQMainWindow
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import thread

class MyTestxEdit1(QTextEdit):
    def __init__(self,parent):
        QTextEdit.__init__(self)
        self.setMouseTracking(True)

class MyTestxEdit(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        self.__qTextEdit=MyTestxEdit1(self)
        self.__qHBoxLayout=QHBoxLayout()
        self.setLayout(self.__qHBoxLayout)
        self.__qHBoxLayout.addWidget(self.__qTextEdit)        
        self.__qTextEdit.installEventFilter(self)


    def eventFilter(self,target,event):
        print "eventFilter "+str(event.type())
        if(event.type()==QEvent.MouseButtonPress):
            print "Mouse was presssed "+str(event.type())
            self.mousePressEvent(event) 
            return True
        return False                   


if __name__ == '__main__':
    app = KQApplication(sys.argv,[])
    mainWindow = KQMainWindow()#loc, splash, pluginFile, noopen, restartArgs)
    s = QSize(800, 600)
    mainWindow.resize(s)    
    testxEdit=MyTestxEdit()
    mainWindow.setCentralWidget(testxEdit)

    mainWindow.show()
    res = app.exec_()
    sys.exit(res)
+1  A: 

Hey,

Try to install the filter on the QTextEdit's viewport instead of the QTextEdit itself...

I don't know python but something like :

self.__qTextEdit.viewport().installEventFilter(self)

I hope it helps !

EDIT :

You should do something like :

MyClassFrm::MyClassFrm()
{
    ...
    // Get your TextEdit from the UI here , or create your TextEdit here....
    // Install the filter
    pMyTextEdit->viewport()->installEventFilter(this);
    ...
}

...

bool MyClassFrm::eventFilter(QObject* pObject, QEvent* pEvent)
{
    if (pEvent->type() == QEvent::MousePressEvent) 
    {
        qDebug() << "Mouse pressed !!";
        // standard event processing
        return QObject::eventFilter(pObject, pEvent);
    }
}

You should be able to make it work, I just tested in a my application, it works... I'm sure you're close ! :) Good luck

Andy M
10'xs It did make the change
jojo
10'xs It is working i have started getting the QEvent::Paint every seconds<br>which prevent me from noticing that the QEvent::MousePressEvent was caught as well 10'xs mate
jojo
Wicked ! Nice job ! cu
Andy M