PyQt4 用户输入验证 - QlineEdit [英] PyQt4 User Input Validation - QlineEdit

查看:77
本文介绍了PyQt4 用户输入验证 - QlineEdit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在理解 PyQt4 的输入验证时遇到了一些麻烦.这是我的第一个 GUI 应用程序,也是第一次使用 PyQt4 框架.我一直在阅读类参考,看起来进行文本验证的首选方法是通过 QRegularExpression 类,但这对于一些简单的输入验证来说似乎有些过分.

I'm having a little trouble understanding input validation with PyQt4. This is my first GUI application and first time working with the PyQt4 framework. I've been reading through the Class reference and it looks like the preferred way to do text validation would be through the QRegularExpression class but that seems excessive for some simple input validation.

我的注册用户类中有一个方法可以将用户添加到 sqlite 数据库中.我还为连接到验证文本的方法的 QlineEdits 创建了一个信号.SQL 输入工作得很好,但由于某种原因,输入验证没有.这不会产生错误.MessageBoxes 只是不弹出.我知道我只创建了一个 SIGNAL 但这只是为了测试.

I have a method in my register user class that adds a user into a sqlite database. I also created a signal for the QlineEdits that connects to a method that validates the text. The SQL input works just fine but for some reason the input validation does not. This does not pull an error. The MessageBoxes just do not pop up. I understand that I only created one SIGNAL but this was just for testing.

def newUser(self):                 #This method adds a new user into the login database and displays a pop up window confirming the entry
    c.execute("INSERT INTO logins(usernames, passwords)VALUES(?,?)", (self.userEdit.text(), self.passEdit.text())) #sql query inserts entries from line edit and pass edit into database
    c.commit() #Save database changes
    self.connect(self.userEdit, QtCore.SIGNAL("textchanged()"), self.validText)


def validText(self):
    if len(self.userEdit.text()) < 4:
        if len(self.passEdit.text()) < 4:
            self.msg = QtGui.QMessageBox.information(self, 'Message', 'Not enough characters!', QtGui.QMessageBox.Ok)       
        else:
            self.msg = QtGui.QMessageBox.information(self, 'Message', 'User added successfully', QtGui.QMessageBox.Ok)

在语义上我知道这是有道理的,但我无法弄清楚我在语法上哪里出错了.有人可以告诉我,除了使用 len 之外,我是否还应该考虑另一个概念?

Semantically I know this makes sense but I can't figure out where I went wrong syntactically. Can someone tell me if there is a another concept I should be looking at besides using len?

提前致谢!

推荐答案

我希望我理解你的问题,所以你在你的应用程序的某个地方有一个 QLineEdit.并且您想阻止用户输入奇怪"字符,例如:~!@#$#%)(& ...等等,根据我在您的问题中读到的内容,您使用从用户那里收集的输入将它发送到数据库中,在这种情况下,如果是一个数据库,您需要避免再次发送我说奇怪"的字符,好吧...如果是这种情况,那么我制作了一个快速应用程序来展示您如何避免这是代码:

I hope that I understand your question, so you got a QLineEdit somewhere in your app. and you want to stop users to enter "strange" characters like: ~!@#$#%)(& ...and so on, well from what I read in your question you use the input that is gathered from the user to send it in a database, which in this case if is a database you need to avoid sending again I say "strange" characters, well... If this is the case then, I made a quick app. to show how you can avoid that here is the code:

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys


class main_window(QDialog):
    def __init__(self):
        QDialog.__init__(self)

        # Create QLineEdit
        le_username = QLineEdit(self)
        le_username.setPlaceholderText("Enter username")
        le_password = QLineEdit(self)
        le_password.setPlaceholderText("Enter password")

        # Create QLabel
        lb_username = QLabel("Username: ")
        lb_password = QLabel("Password: ")

        # Adding a layout
        self.setLayout(QVBoxLayout())


        # Adding widgets to layout
        self.layout().addWidget(lb_username)
        self.layout().addWidget(le_username)


        self.layout().addWidget(lb_password)
        self.layout().addWidget(le_password)


        #!! ReGex implementation !!
        # For more details about ReGex search on google: regex rules or something similar 
        reg_ex = QRegExp("[a-z-A-Z_]+")
        le_username_validator = QRegExpValidator(reg_ex, le_username)
        le_username.setValidator(le_username_validator)
        #!! ReGex implementation End !!


        #.......
        self.setMinimumWidth(200)
        self.setWindowTitle("ReGEX Validator in Python with Qt Framework")

app = QApplication(sys.argv)
dialog = main_window()
dialog.show()
sys.exit(app.exec_())

我希望这能帮助您弄清楚如何在 QLineEdit 或任何您根据字符获得用户输入的地方过滤用户输入...

I hope that this help you out to figure how to filter user input in a QLineEdit, or anywhere where you got an user input based on characters...

这篇关于PyQt4 用户输入验证 - QlineEdit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆