7

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!

Reagan Kirby
  • 81
  • 1
  • 1
  • 4
  • Step 1 is to make sure your signal is firing at all using a debugger or at least using `print`. The signal is case-sensitive I believe, so you would want textChanged() rather than textchanged(). Why not also connect the signal to your validText slot right when you initialize the userEdit? I'm not sure where/how you call newUser(), but presumably you want to validate the username and password **before** you commit anything to your database. – nb1987 Aug 25 '15 at 04:04
  • I appreciate the answer. These are all good points and I will definitely rework the problem. I don't know why I set it to commit changes before validation. @nb1987 – Reagan Kirby Aug 25 '15 at 14:42

1 Answers1

12

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

  • Thank you for your answer! So ReGrex and QValidator are the way to go when validating Qline input in PyQt4? This did answer my question. My apologies for the delayed response. – Reagan Kirby Aug 30 '15 at 21:46
  • Glad to hear that helped you out –  Aug 31 '15 at 06:09