【问题标题】:Clear a lineEdit inputMask in python在python中清除lineEdit inputMask
【发布时间】:2021-05-22 02:45:57
【问题描述】:

如何在 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_())

我应该用什么方法清除lineEdit 掩码,因为clearMask 和其他都不起作用。

【问题讨论】:

    标签: python pyqt qlineedit


    【解决方案1】:

    您必须将一个空字符串传递给 setInputMask() 正如文档指出的那样:

    如果没有设置掩码,inputMask() 返回一个空字符串。

    e3.setInputMask("")
    

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

    【讨论】:

      【解决方案2】:

      只使用一个空字符串:

      setInputMask('')
      

      我强烈建议您阅读文档,而不是随意尝试,因为很明显 clearMask() 与它完全无关,而 inputMask 明确指出:

      通过传递一个空字符串 ("") 取消设置掩码并返回到正常的 QLineEdit 操作。

      【讨论】:

      猜你喜欢
      • 2014-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-17
      • 2012-01-04
      • 1970-01-01
      • 2022-06-25
      • 1970-01-01
      相关资源
      最近更新 更多