pyqt QLineEdit 详细用法

一  QLineEdit回显格式       

       QLineEdit的文本有四种显示方式:

     (1)正常显示,

     (2)输入但不显示文本,

     (3)密码格式显示,

     (4)类似于在linux下输入密码也不显示。

       Qt代码中有声明:

    NoEcho = 1
    Normal = 0
    Password = 2
    PasswordEchoOnEdit = 3

         Qt助手的解释

       enum QLineEdit::EchoMode

       This enum type describes how a line edit should display its contents.

Constant

Value

Description

QLineEdit::Normal

0

Display characters as they are entered. This is the default.

QLineEdit::NoEcho

1

Do not display anything. This may be appropriate for passwords where even the length of the password should be kept secret.

QLineEdit::Password

2

Display platform-dependent password mask characters instead of the characters actually entered.

QLineEdit::PasswordEchoOnEdit

3

Display characters as they are entered while editing otherwise display characters as with Password.

        例如下面的demo

pyqt QLineEdit 详细用法

        代码如下:

from PyQt5.QtWidgets import *
import sys

class QLineEditEchoMode(QWidget) :
    def __init__(self):
        super(QLineEditEchoMode,self).__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('文本输入框的回显模式')
        self.setFixedWidth(290)

        formLayout = QFormLayout()

        normalLineEdit = QLineEdit()
        noEchoLineEdit = QLineEdit()
        passwordLineEdit = QLineEdit()
        passwordEchoOnEditLineEdit = QLineEdit()

        formLayout.addRow("Normal",normalLineEdit)
        formLayout.addRow("NoEcho", noEchoLineEdit)
        formLayout.addRow("Password",passwordLineEdit)
        formLayout.addRow("PasswordEchoOnEdit",passwordEchoOnEditLineEdit)

        # 设置lineEdit提示信息
        normalLineEdit.setPlaceholderText("文本回显")
        noEchoLineEdit.setPlaceholderText("不回显")
        passwordLineEdit.setPlaceholderText("密码模式")
        passwordEchoOnEditLineEdit.setPlaceholderText("密码不显示")

        normalLineEdit.setEchoMode(QLineEdit.Normal)
        noEchoLineEdit.setEchoMode(QLineEdit.NoEcho)
        passwordLineEdit.setEchoMode(QLineEdit.Password)
        passwordEchoOnEditLineEdit.setEchoMode(QLineEdit.NoEcho)

        self.setLayout(formLayout)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = QLineEditEchoMode()
    main.show()
    sys.exit(app.exec_())

        通过函数setEchoMode设置了QLineEdit的回显模式,setPlaceholderText设置LineEdit的提示信息。

 

二  QLineEdit输入限制类型

       在有些场合,需要限制输入的类型,例如密码不能输入汉字,年龄不能为负数,单科总分不超过150等,那么在开发的时候就需要做一些限制。Qt提供了校验器,可以解决这种限制输入的问题,当然最好的做法还是正则表达式。例如下面的demo:

from PyQt5.QtWidgets import *
from PyQt5.QtGui import QIntValidator,QDoubleValidator, QRegExpValidator
from PyQt5.QtCore import QRegExp
import sys

class QLineEditValidator(QWidget):
    def __init__(self):
        super(QLineEditValidator,self).__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('校验器')

        # 创建表单布局
        formLayout = QFormLayout()

        intLineEdit = QLineEdit()
        doubleLineEdit = QLineEdit()
        validatorLineEdit = QLineEdit()

        formLayout.addRow('整数类型', intLineEdit)
        formLayout.addRow('浮点类型', doubleLineEdit)
        formLayout.addRow('数字和字母',validatorLineEdit)

        intLineEdit.setPlaceholderText('整型')
        doubleLineEdit.setPlaceholderText('浮点型')
        validatorLineEdit.setPlaceholderText('字母和数字')

        # 整数校验器 [1,99]
        intValidator = QIntValidator(self)
        intValidator.setRange(1,99)

        # 浮点校验器
        #设置浮点数为 -180到180,四位小数点,QDoubleValidator有bug, 不建议使用
        reg1 = QRegExp('^-?(180|1?[0-7]?\\d(\\.\\d{1,4})?)$')
        validator1 = QRegExpValidator(self)
        validator1.setRegExp(reg1)

        # 字符和数字
        reg2 = QRegExp('[a-zA-z0-9]+$')
        validator2 = QRegExpValidator(self)
        validator2.setRegExp(reg2)

        # 设置校验器
        intLineEdit.setValidator(intValidator)
        doubleLineEdit.setValidator(validator1)
        validatorLineEdit.setValidator(validator2)

        self.setLayout(formLayout)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = QLineEditValidator()
    main.show()
    sys.exit(app.exec_())

     运行结果:

pyqt QLineEdit 详细用法

     整数限制为1--99

   浮点数为 -180到180,四位小数点

     四种校验器QIntValidator,QDoubleValidator,QRegExpValidator, 和QRegularExpressionValidator。

 

三  QLineEdit掩码输入

     Qt提供了以下掩码输入规定:

A    ASCII字母字符是必须输入的(A-Z、a-z)
a    ASCII字母字符是允许输入的,但不是必需的(A-Z、a-z)
N    ASCII字母字符是必须输入的(A-Z、a-z、0-9)
n    ASII字母字符是允许输入的,但不是必需的(A-Z、a-z、0-9)
X    任何字符都是必须输入的
x    任何字符都是允许输入的,但不是必需的
9    ASCII数字字符是必须输入的(0-9)
0    ASCII数字字符是允许输入的,但不是必需的(0-9)
D    ASCII数字字符是必须输入的(1-9)
d    ASCII数字字符是允许输入的,但不是必需的(1-9)
#    ASCI数字字符或加减符号是允许输入的,但不是必需的
H    十六进制格式字符是必须输入的(A-F、a-f、0-9)
h    十六进制格式字符是允许输入的,但不是必需的(A-F、a-f、0-9)
B    二进制格式字符是必须输入的(0,1)
b    二进制格式字符是允许输入的,但不是必需的(0,1)
>    所有的字母字符都大写
<    所有的字母字符都小写
!    关闭大小写转换
\    使用"\"转义上面列出的字符

      代码demo如下:

from PyQt5.QtWidgets import *
import sys

class QLineEditMask(QWidget) :
    def __init__(self):
        super(QLineEditMask,self).__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('用掩码限制QLineEdit控件的输入')
        formLayout = QFormLayout()

        ipLineEdit = QLineEdit()
        macLineEdit = QLineEdit()
        dateLineEdit = QLineEdit()
        licenseLineEdit = QLineEdit()
        # 192.168.123.45
        ipLineEdit.setInputMask('000.000.000.000;_')
        macLineEdit.setInputMask('HH:HH:HH:HH:HH:HH;_')
        dateLineEdit.setInputMask('0000-00-00')
        licenseLineEdit.setInputMask('>AAAAA-AAAAA-AAAAA-AAAAA-AAAAA;#')

        formLayout.addRow('数字掩码',ipLineEdit)
        formLayout.addRow('Mac掩码', macLineEdit)
        formLayout.addRow('日期掩码',dateLineEdit)
        formLayout.addRow('许可证掩码',licenseLineEdit)

        self.setLayout(formLayout)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = QLineEditMask()
    main.show()
    sys.exit(app.exec_())

    运行结果:

pyqt QLineEdit 详细用法

 

   

上一篇:java构造器


下一篇:c++浅拷贝和深拷贝学习案例