Search code examples
pythonpyqt4qlineedit

How to turn a Clickable QLineEdit to a normal one after first click?


I have a clickable lineEdit:

> class ClickableLineEdit(QtGui.QLineEdit): #This is the Class which let you to have a clickable QLineEdit
      clicked = QtCore.pyqtSignal()
      def mousePressEvent(self, event):
            self.clicked.emit()
            QtGui.QLineEdit.mousePressEvent(self, event)

Which it clears the default text after a click:

        self.lineEdit = ClickableLineEdit(Form)
        self.lineEdit.setText(_translate("Form", "0.14286", None)) #Carrying the default value of QLineEdit.
        self.lineEdit.clicked.connect(self.lineEdit_refrac.clear)

How to change my code to set the QlineEdit's behavior to normal after the first click?

It means after the lineEdit is cleared, now I want the user can click for editing purposes on the input text.


Solution

  • In this case I think that it is not necessary to implement a signal, only the use of a flag.

    class LineEdit(QtGui.QLineEdit):
        def __init__(self, *args, **kwargs):
            super(LineEdit, self).__init__(*args, **kwargs)
            self.flag = False
    
        def mousePressEvent(self, event):
            if not self.flag:
                self.clear()
            self.flag = True
            QtGui.QLineEdit.mousePressEvent(self, event)
    
    # ...
    
        self.lineEdit = LineEdit(Form)
        self.lineEdit.setText(_translate("Form", "0.14286", None)) #Carrying the default value of QLineEdit.
        # self.lineEdit.clicked.connect(self.lineEdit_refrac.clear)