在python中清除lineEdit输入掩码

2024-09-28 21:27:32 发布

您现在位置:Python中文网/ 问答频道 /正文

如何在python中清除掩码?我正在使用setInputMaskqLineEdit中插入一个掩码,但在一段时间后,我想清除lineEdit(移除掩码)。我试图使用clearMaskclearsetText(""),但都不起作用

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_())

由于clearMask和其他方法不起作用,我应该使用什么方法来清除lineEdit掩码


Tags: fromimportselfsyspyqt5floclear掩码
2条回答

只需使用空字符串:

setInputMask('')

我强烈建议您阅读文档,而不是尝试随机的事情,因为很明显^{}与此毫无关系,而^{}明确指出:

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

您必须向^{}传递一个空字符串,正如文档所指出的:

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

e3.setInputMask("")

注意:clearMask()清除另一种类型的掩码:https://doc.qt.io/qt-5/qwidget.html#setMask

相关问题 更多 >