How to validate QDateEdit
-
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)
-
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)
@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 againstself.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. -
@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)
-
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.
-
Use pdb directly.