PyQt事件处理和QErrorMessage:检测点击的来源 [英] PyQt Event Handling and QErrorMessage: detect the source of the click

查看:120
本文介绍了PyQt事件处理和QErrorMessage:检测点击的来源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python和Qt的新手,但是我遇到以下问题: 我已经重写了class mywin(QMainWindow):的事件处理程序,因此,当我单击它时,应该执行命令. Howewer,当命令返回错误时,我想使用QErrorMessage显示错误消息.但是,当我单击错误消息"的确定"按钮时,会注册另一个click事件,并且该命令会重新执行,错误并显示新的错误消息,因此我永远无法退出错误消息(每次关闭时,另一个重新打开).

I'm new to Python and Qt, and I have the following problem: I have rewritten the event handler of class mywin(QMainWindow):, so that when I click on it, a command should execute. Howewer, when the command returns an error, I would like to show an error message using QErrorMessage. Howewer, when I click on the OK button of the Error Message, an other click event is registred and the command reexecutes, errors and shows a new error message, so I can't ever quit the error messages (Everytime I close on, an other one reopens).

def eventFilter(self, source, event):
    if event.type() == QEvent.MouseButtonPress:
        if isinstance(source, QWidget):
            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)

def testCommand(self, textClicked=None):
            #Command executing and error finding
            if error:   
             errorMessage=QErrorMessage(self)
             errorMessage.showMessage(a)

这是eventFilter的记录

Here's the registring of the eventFilter

if __name__ == '__main__': 
    app = QApplication(sys.argv)
    print "OS Name:"+os.name
    main = mywin()
    main.show()
    app.installEventFilter(main)
    sys.exit(app.exec_())

如果我登录

  • 单击文本区域的源,我得到:<PyQT4.QtGui.QWidget object at 0x000000000028B30D0>
  • self.textEdit,我得到<PyQT4.QtGui.QTextEdit object at 0x000000000028B3268>
  • the source on a click on the text region, I get: <PyQT4.QtGui.QWidget object at 0x000000000028B30D0>
  • self.textEdit, I get <PyQT4.QtGui.QTextEdit object at 0x000000000028B3268>

installEventFilter的文档:

Doc of installEventFilter: http://harmattan-dev.nokia.com/docs/platform-api-reference/xml/daily-docs/libqt4/qobject.html#installEventFilter

推荐答案

首先,您应该在注册事件过滤器的地方显示代码.
其次,您正在验证这是否是您要过滤的内容不是很好的事件.您应该验证特定的小部件而不是类型,因此它应该类似于:

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)



就像我怀疑的那样:由于您已在QApplication对象上安装了事件过滤器,因此您正在为所有小部件缓存所有事件.在小部件上注册事件过滤器,以跟踪鼠标事件.在事件过滤器中,请使用我上面写的使用条件.



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.

这篇关于PyQt事件处理和QErrorMessage:检测点击的来源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆