How to throw error message when QPushbutton is clicked if LineEdit fields are empty
-
Hi guys, I'm writing a function and I'm struggling with how to throw an error message when the user click on save button if input fields are empty. Please I need assistance. Below is what I have done. Thanks in advance```
def ADD_NEW(self): self.db = MySQLdb.connect(host='localhost',user='root', password='1234RT', db='customers') self.cur = self.db.cursor() fname = self.lineEdit_27.text() sname = self.lineEdit_8.text() email = self.lineEdit_11.text() self.cur.execute('''INSERT INTO details (user_fname, user_sname, user_email) VALUES (%s,%s,%s)''', (fname, sname, email)) self.db.commit() self.lineEdit_27.setText('') self.lineEdit_8.setText('') self.lineEdit_11.setText('') def show_message(title='Prompt', message='New Record Inserted'): QMessageBox.information(None, title, message) show_message()
-
Hi,
From a usability point of view, you should rather disable the button until there's something in the line edit.
Making them going back and forth is less is less user friendly.
-
Hi
You could start with setting the button disabled
(using setEnabled(false) )
and hook the Linedits signal
https://doc.qt.io/qtforpython-5/PySide2/QtWidgets/QLineEdit.html#PySide2.QtWidgets.PySide2.QtWidgets.QLineEdit.textChangedfor Text is changed to your own slot.
Then in this slot, then validate the text and if ok, then enable the buttons
if text is empty. (use isEmpty() function of QString) then also disable button here.
So if user deletes the text they go back to disabled. -
@mrjj Thanks for your response but the example to the link is not clear to me since, I'm new to Pyqt5 and python.
Below is what i tried doing but not sure it's correct. Any help please?sender = self.lineEdit_27.text() receiver = self.lineEdit_8.text() dispatch_name = self.lineEdit_11.text() leInput = (sender,receiver,dispatch_name) self.btnButton.setDisable(True) self.leInput.textChanged.connect(self.disableButton) def disableButton(self): if len(self.leInput.text()) > 0: self.btnButton.setDisable(False) disableButton()
-
@LT-K101 said in How to throw error message when QPushbutton is clicked if LineEdit fields are empty:
leInput = (sender,receiver,dispatch_name)
You are trying to connect strings.
What you need to do is connect each QLineEdit's textChanged signal to your slot and check each of them.
for line_edit in [self.lineEdit_27.text(), self.lineEdit_8.text(), self.lineEdit_11.text(), ]: line_edit.textChanged.connect(self.disableButton)
Please consider using tools like black for Python, flake8, isort, bandit and friends to keep your code clean and tidy.
Tools like pre-commit can help you with that as well.
-
@LT-K101 said in How to throw error message when QPushbutton is clicked if LineEdit fields are empty:
self.btnButton.setDisable(True)
self.btnButton.setDisable(False)
What happens when you run your code? There is no such method as
QPushButton.setDisable()
. There issetDisabled()
orsetEnabled()
.disableButton()
This is not the way to call a member function in Python.