2009-11-23 33 views
2

我有問題過濾"mousePressEvent"installEventFilter過濾mousePressEvent與installEventFilter

MyTestxEdit是持有QTextEdit 我想要的QTextEdit所有事件將被MyTestxEdit 來處理我已經使用了installEventFilter 這一招一個widget適用於keyPressEvent等事件,但不處理mousePressEvent 我做錯了什麼?

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)  
+0

嗨,你的問題幫了我很多。我可以問你'eventFilter'中的'self.mousePressEvent(event)'是什麼嗎?我已經嘗試了類似的例子,但我無法分辨在評論該行時是否有區別。 – Aleksandar 2014-01-17 09:44:10

回答

5

嘗試在QTextEdit's視代替QTextEdit本身安裝過濾器...

我不知道蟒蛇,但喜歡的事:

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

我希望它能幫助!

你應該這樣做:

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); 
    } 
} 

你應該能夠使它工作,我只是在我的應用程序進行測試,它的工作原理...我敢肯定,你靠近!

+0

10'xs 它確實做出改變 – jojo 2009-11-23 20:10:33

+0

10'xs 這是我的工作已經開始得到的QEvent ::塗料每秒鐘
它阻止我注意到,在QEvent的:: MousePressEvent被抓獲以及 10' xs mate – jojo 2009-11-23 20:16:49

+0

邪惡!不錯的工作 !銅 – 2009-11-23 20:29:09