0

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.

Ash
  • 263
  • 3
  • 14

2 Answers2

1

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)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
1

In the method that is called when you click the QLineEdit the first time, you can disconnect it. So turns this:

self.lineEdit.clicked.connect(self.lineEdit_refrac.clear)

Into:

self.lineEdit.clicked.connect(self.clear_line_edit)

def clear_line_edit(self):
    self.lineEdit_refrac.clear() # does what you wanted
    self.lineEdit.clicked.disconnect(self.clear_line_edit) # then ensures the click does not call this method anymore
Guimoute
  • 4,407
  • 3
  • 12
  • 28