【问题标题】:mouseDoubleClickEvent with QLineEdit带有 QLineEdit 的 mouseDoubleClickEvent
【发布时间】:2018-03-21 16:58:37
【问题描述】:

我怎样才能让QLineEdit 默认无法使用,但在收到mouseDoubleClickEvent() 时启用?

如何实现mouseDoubleClickEvent()

当我尝试以下操作时,总是会收到“参数不足”的错误:

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

【问题讨论】:

    标签: python python-2.7 pyqt pyqt4 mouseevent


    【解决方案1】:

    你不能用语句设置那个事件:

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

    有两种可能的选择:

    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_())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-09
      • 1970-01-01
      • 1970-01-01
      • 2013-08-04
      • 1970-01-01
      • 2017-08-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多