Search code examples
pythonpyqtqlineedit

Clear a lineEdit inputMask in python


How can I clear a mask in python? I'm using setInputMask to insert a mask in a qLineEdit but after a period i want to clear the lineEdit (remove the mask). I tried to use clearMask, clear, and setText("") but none worked.

The MVCE:

from PyQt5.QtWidgets import QApplication,QLineEdit,QWidget,QFormLayout
from PyQt5.QtGui import QIntValidator,QDoubleValidator,QFont
from PyQt5.QtCore import Qt
import sys

class lineEditDemo(QWidget):
        def __init__(self,parent=None):
                super().__init__(parent)

                e3 = QLineEdit()
                e3.setInputMask("+99_9999_999999")
                e3.clearMask()
                e3.clear()
                e3.setText("")
                
                flo = QFormLayout()
                flo.addRow("Input Mask",e3)
                self.setLayout(flo)
                self.setWindowTitle("QLineEdit Example")

if __name__ == "__main__":
        app = QApplication(sys.argv)
        win = lineEditDemo()
        win.show()
        sys.exit(app.exec_())

What method should I use to clear the lineEdit mask since clearMask and others don't worked.


Solution

  • You have to pass an empty string to setInputMask() As the docs point out:

    If no mask is set, inputMask() returns an empty string.

    e3.setInputMask("")
    

    Note: clearMask() clears another type of mask: https://doc.qt.io/qt-5/qwidget.html#setMask