使用QLineEdit的mouseDoubleClickEvent [英] mouseDoubleClickEvent with QLineEdit

查看:244
本文介绍了使用QLineEdit的mouseDoubleClickEvent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何默认情况下无法使QLineEdit失效,但是在收到mouseDoubleClickEvent()时将其启用?

How can I have a QLineEdit unabled by default but that becomes enabled when receives a mouseDoubleClickEvent()?

如何实现mouseDoubleClickEvent()?

尝试以下操作时,总是会收到参数不足"错误:

I always get the error "not enough arguments" when I try something like:

if self.MyQLineEdit.mouseDoubleClickEvent() == True:
    do something

推荐答案

您不能使用以下语句设置该事件:

You can not set that event with the statement:

if self.MyQLineEdit.mouseDoubleClickEvent () == True:

有2种可能的选择:

  1. 第一个是从QLineEdit继承:


import sys

from PyQt4 import QtGui

class LineEdit(QtGui.QLineEdit):
    def mouseDoubleClickEvent(self, event):
        print("pos: ", event.pos())
        # do something

class Widget(QtGui.QWidget):
    def __init__(self, *args, **kwargs):
        QtGui.QWidget.__init__(self, *args, **kwargs)
        le = LineEdit()
        lay = QtGui.QVBoxLayout(self)
        lay.addWidget(le)

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

  1. 安装事件过滤器:


import sys

from PyQt4 import QtGui, QtCore

class Widget(QtGui.QWidget):
    def __init__(self, *args, **kwargs):
        QtGui.QWidget.__init__(self, *args, **kwargs)
        self.le = QtGui.QLineEdit()        
        lay = QtGui.QVBoxLayout(self)
        lay.addWidget(self.le)

        self.le.installEventFilter(self)

    def eventFilter(self, watched, event):
        if watched == self.le and event.type() == QtCore.QEvent.MouseButtonDblClick:
            print("pos: ", event.pos())
            # do something
        return QtGui.QWidget.eventFilter(self, watched, event)

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

这篇关于使用QLineEdit的mouseDoubleClickEvent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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