gpt4 book ai didi

python - 使用 installEventFilter 过滤 mousePressEvent

转载 作者:太空宇宙 更新时间:2023-11-03 13:53:49 31 4
gpt4 key购买 nike

我在使用 installEventFilter 过滤 "mousePressEvent" 时遇到问题

MyTestxEdit 是一个包含 QTextEdit 的小部件我希望 QTextEdit 的所有事件都由 MyTestxEdit 处理我使用了 installEventFilter此技巧适用于 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)

最佳答案

尝试将过滤器安装在 QTextEdit 的 视口(viewport)上而不是 QTextEdit 本身...

我不知道 python,但像这样:

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

你应该能够让它工作,我刚刚在我的应用程序中测试过,它工作......我相信你很接近!

关于python - 使用 installEventFilter 过滤 mousePressEvent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1785251/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com