Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How to throw error message when QPushbutton is clicked if LineEdit fields are empty
Forum Updated to NodeBB v4.3 + New Features

How to throw error message when QPushbutton is clicked if LineEdit fields are empty

Scheduled Pinned Locked Moved Unsolved Qt for Python
7 Posts 4 Posters 1.3k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    LT-K101
    wrote on last edited by
    #1

    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()
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      L 1 Reply Last reply
      0
      • SGaistS SGaist

        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.

        L Offline
        L Offline
        LT-K101
        wrote on last edited by
        #3

        @SGaist Thanks for your response. Do you please have any article to your recommendation? I would be glad if you assist me with how to go about it.

        mrjjM 1 Reply Last reply
        0
        • L LT-K101

          @SGaist Thanks for your response. Do you please have any article to your recommendation? I would be glad if you assist me with how to go about it.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @LT-K101

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

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

          L 1 Reply Last reply
          1
          • mrjjM mrjj

            @LT-K101

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

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

            L Offline
            L Offline
            LT-K101
            wrote on last edited by
            #5

            @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()
            
            
            
            JonBJ 1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by SGaist
              #6

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

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • L LT-K101

                @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()
                
                
                
                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @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 is setDisabled() or setEnabled().

                disableButton()

                This is not the way to call a member function in Python.

                1 Reply Last reply
                1

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved