PyQt Event Handling and QErrorMessage: detect the source of the click

ⅰ亾dé卋堺 提交于 2019-12-08 18:33:25

First of all you should show the code where you are registering event filter.
Secondly way you are verifying that this is the event you what to filter is not so good. You should verify specific widget not the type, so it should be something like:

def eventFilter(self, source, event):
    if event.type() == QEvent.MouseButtonPress:
        if source == self.txtEditor :
            pos=event.pos()
            cursor=self.txtEditor.cursorForPosition(pos)
            cursor.select(QTextCursor.WordUnderCursor)
            txtClicked=cursor.selectedText()
            self.testCommand(str(txtClicked))
    return QMainWindow.eventFilter(self, source, event)


Edit:
It is as I suspected: You are caching ALL events for ALL widgets since you have installed event filter on QApplication object. Register event filter on widget you what to track mouse events for. In event filter use use condition as I wrote above.

Shouldn't source argument be different for click events coming from mywin and QErrorMessage objects? If yes, you could check for it and prevent the reexecution.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!