Mutante
Mutante

Reputation: 278

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.

Upvotes: 0

Views: 1007

Answers (2)

musicamante
musicamante

Reputation: 48260

Just use an empty string:

setInputMask('')

I strongly suggest you to read the documentation instead of trying random things, as it's clear that clearMask() has absolutely nothing to do with it, while inputMask clearly states:

Unset the mask and return to normal QLineEdit operation by passing an empty string ("").

Upvotes: 2

eyllanesc
eyllanesc

Reputation: 243973

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

Upvotes: 3

Related Questions