简体   繁体   English

PyQt:仅用于整数或仅用于字符串和长度限制的 QLineEdit 输入掩码

[英]PyQt: QLineEdit input mask for only Integer or only String and length restriction

These are the 2 questions(both can be solved by InputMask?)这是两个问题(都可以通过 InputMask 解决?)

  1. I want to restrict the user input to 16 characters only我想将用户输入限制为仅 16 个字符
  2. In a field like 'Age/ID', I would like the user's input to be integer only, if the user enters a string it must not be accepted or the user must not be able to type in a string in the first place.在像“年龄/ID”这样的字段中,我希望用户的输入只能是整数,如果用户输入一个字符串,则它不能被接受,或者用户首先不能输入字符串。

I'm not sure how I can implement the first part in real-time,ie, the user types a max of 16, nothing beyond 16 appears.我不确定如何实时实现第一部分,即用户输入最多 16 个,不会出现超过 16 个。

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)

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:相比之下,第二个 QIntValidator 问题仅适用于等于2147483647的最大值,因为它是最大整数:2**31-1,解决方案是使用正则表达式:

rx = QRegExp("\d+")
self.lineedit_15.setValidator(QRegExpValidator(rx))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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