PyQt:仅用于整数或仅用于字符串和长度限制的 QLineEdit 输入掩码 [英] PyQt: QLineEdit input mask for only Integer or only String and length restriction
问题描述
这是两个问题(都可以通过InputMask解决?)
These are the 2 questions(both can be solved by InputMask?)
- 我想将用户输入限制为仅 16 个字符
- 在像Age/ID"这样的字段中,我希望用户的输入只能是整数,如果用户输入一个字符串,则不能被接受,或者用户不能在第一个输入字符串
我不确定如何实时实现第一部分,即用户输入最多 16 个,不会出现超过 16 个.
I'm not sure how I can implement the first part in real-time,i.e., the user types a max of 16, nothing beyond 16 appears.
这是我的问题第二部分的代码(不起作用):
This is my code(not working) for the 2nd part of the question:
self.onlyInt = QIntValidator()
self.lineEdit_15.setValidator(self.onlyInt)
det15=str(self.lineEdit_15.text())
list_val.append(det15)
推荐答案
要解决第一个问题,我们只需要建立一个最大尺寸:
To solve the first question we only have to establish a maximum size:
self.lineedit_15.setMaxLength(16)
相比之下,第二个 QIntValidator 问题只适用于等于 2147483647
的最大值,因为它是最大整数:2**31-1,解决方案是使用正则表达式:
In contrast the second QIntValidator question only works up to a maximum equal to 2147483647
since it is the maximum integer: 2**31-1, The solution is to use regular expressions:
rx = QRegExp("\d+")
self.lineedit_15.setValidator(QRegExpValidator(rx))
这篇关于PyQt:仅用于整数或仅用于字符串和长度限制的 QLineEdit 输入掩码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!