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 validate QDateEdit
Forum Update on Monday, May 27th 2025

How to validate QDateEdit

Scheduled Pinned Locked Moved Unsolved Qt for Python
6 Posts 3 Posters 968 Views
  • 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,
    I'm trying to validate a QDateEdit but it does not seem working. I want to check if the QDateEdit selected is today's date, if it is the label_1119 should be invisible but if the QDateEdit date is not today's date the label_1119 should be visible .Is it possible to validate QDateEdit? Please I need help around this. Thanks in advance. Below is what I tried.

       newDate = ""    
       now = QDate.currentDate()
       secondDate = self.ui.second_dateEdit.setDate(now)
    
    
       if self.ui.second_dateEdit.date().toString() != secondDate:
           self.ui.label_1119.setText(self.ui.second_dateEdit.date().toString())
           self.ui.label_1119.setVisible(True)
       elif  self.ui.second_dateEdit.date().toString() == secondDate:
                self.ui.label_1119.setText(newDate)
                self.ui.label_1119.setVisible(False)
       else:
                self.ui.label_1119.setVisible(False)
    
    JonBJ 1 Reply Last reply
    0
    • L LT-K101

      Hi,
      I'm trying to validate a QDateEdit but it does not seem working. I want to check if the QDateEdit selected is today's date, if it is the label_1119 should be invisible but if the QDateEdit date is not today's date the label_1119 should be visible .Is it possible to validate QDateEdit? Please I need help around this. Thanks in advance. Below is what I tried.

         newDate = ""    
         now = QDate.currentDate()
         secondDate = self.ui.second_dateEdit.setDate(now)
      
      
         if self.ui.second_dateEdit.date().toString() != secondDate:
             self.ui.label_1119.setText(self.ui.second_dateEdit.date().toString())
             self.ui.label_1119.setVisible(True)
         elif  self.ui.second_dateEdit.date().toString() == secondDate:
                  self.ui.label_1119.setText(newDate)
                  self.ui.label_1119.setVisible(False)
         else:
                  self.ui.label_1119.setVisible(False)
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @LT-K101 said in How to validate QDateEdit:

      secondDate = self.ui.second_dateEdit.setDate(now)

      Assuming this is QDateEdit.setDate() it does not return any value at all. So what is in secondDate, and how in the world do you expect to compare it against self.ui.second_dateEdit.date().toString()?

      Please I need help around this.

      No, you need to make more effort for really simple things. You are quite capable of using print() statements or a debugger on your code for this.

      I want to check if the QDateEdit selected is today's date

      QDate.currentDate() returns a date. self.ui.second_dateEdit.date() returns a date.

      1 Reply Last reply
      0
      • L Offline
        L Offline
        LT-K101
        wrote on last edited by
        #3

        @JonB I tried the code below but when I add say four records(rows) all the labels that displays the lineEdit details works fine but none of the labels for the dateEdit works.

        Record = 0
        
        self.ui.add_btn.clicked.connect(self.add_info)
        self.ui.preview_btn.clicked.connect(self.previewDetails)
        
        def add_info(self):
            
            global Record
        
            self.ui.lineEdit.setVisible(True)
            self.ui.lineEdit_2.setVisible(True)
            self.ui.lineEdit_3.setVisible(True)
            self.ui.dateEdit.setVisible(True)
            
            Record = 1        
        		
        		
        def previewDetails(self):
        	
            currentDate = datetime.date.today().strftime("%Y-%m-%d")
        	
            if self.ui.lineEdit.text().strip() != '':
               self.ui.label_1104.setText(self.ui.lineEdit.text())
               self.ui.label_1104.setVisible(True)
            else:
                 self.ui.label_1104.setVisible(False)
        
            if self.ui.lineEdit_2.text().strip() != '':
               self.ui.label_1109.setText(self.ui.lineEdit.text())
               self.ui.label_1109.setVisible(True)
            else:
                    self.ui.label_1109.setVisible(False)
        
            if self.ui.lineEdit_3.text().strip() != '':
               self.ui.label_1114.setText(self.ui.lineEdit.text())
               self.ui.label_1114.setVisible(True)
            else:
                 self.ui.label_1114.setVisible(False)
        	
            if Record== 1 and self.ui.dateEdit.date().toPyDate().strftime("%Y-%m-%d") != currentDate:
               self.ui.label_1119.setText(self.ui.dateEdit.date().toPyDate().strftime("%Y-%m-%d"))
               self.ui.label_1119.setVisible(True)
        
            else:
                self.ui.label_1119.setVisible(False)
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          As @JonB already wrote, use the debugger to check what happens with your if. Or just print both values to see what you are dealing with.

          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,

            As @JonB already wrote, use the debugger to check what happens with your if. Or just print both values to see what you are dealing with.

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

            @SGaist i can not used the pycharm debugger anymore. Below is what displays when i use the debugger.

            Screenshot (3).png

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Use pdb directly.

              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
              0

              • Login

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