PyQt4 User Input Validation - QlineEdit

2020-03-19 06:27发布

问题:

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.

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)

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?

Thanks in advance!

回答1:

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_())

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...