带有QLineEdit的mouseDoubleClickEvent

时间:2018-03-21 16:58:37

标签: python python-2.7 pyqt pyqt4 mouseevent

如何在默认情况下QLineEdit无效,但在收到mouseDoubleClickEvent()时会启用?

如何实施mouseDoubleClickEvent()

我总是得到错误"没有足够的论据"当我尝试类似的东西时:

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

1 个答案:

答案 0 :(得分:1)

您无法使用以下语句设置该事件:

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

有两种可能的选择:

  1. 第一个是继承QLineEdit
  2. 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. 安装事件过滤器:
    2. 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_())