我有问题过滤"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)
嗨,你的问题帮了我很多。我可以问你'eventFilter'中的'self.mousePressEvent(event)'是什么吗?我已经尝试了类似的例子,但我无法分辨在评论该行时是否有区别。 – Aleksandar 2014-01-17 09:44:10