简体   繁体   中英

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.

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

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 ("").

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM