How to clear QLineEdit without triggering signal

Question:

QLineEdit triggers a signal on .clear() or .setText() method.
So every time those methods are used to stop LineEdit from emitting the signal I need to .blockSignals(True) and then .blockSignals(False).
I wonder if there is a way around it?

from PyQt4 import QtCore, QtGui    
app = QtGui.QApplication([])

class LineEdit(QtGui.QLineEdit):
    def __init__(self, *args, **kwargs):
        super(LineEdit, self).__init__()
        self.setText('Some Text')
        self.textChanged.connect(self.printMessage)
        self.show()
    def printMessage(self):
        print 'Triggered!'

line=LineEdit()
line.clear()
sys.exit(app.exec_())
Asked By: alphanumeric

||

Answers:

Have you considered QLineEdit::textEdited? It doesn’t emit signals when the text is changed programatically.

Answered By: James

Above answer is probably better, but maybe some scenarios this solution is also useful:

def function_that_doesnt_trigger_textchanged(self):
    self.in_this_function = True
    self.textbox.change_text()
    self.in_this_function = False

def on_text_changed(self):
    if self.in_this_function:
        return
    do_thing_on_text_changed()
Answered By: Julian Matthews
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.