Log in

View Full Version : QDateEdit bug



iAl
11th April 2021, 15:11
I have noticed that there is a bug in QDateEdit widget, I am using Qt Designer 5.15.2 to design QPushButton, QDateEdit and QLineEdit with python 3.9.2:


from PyQt5.QtWidgets import *
from PyQt5.uic import loadUiType
import sys
Ui_MainWindow,_ = loadUiType('tst.ui')
class TEST(Ui_MainWindow, QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setupUi(self)
self.btn.clicked.connect(self.enterDATE) #QPushButton signal
def enterDATE(self):
Date_2021_03_26 = "{0}-{1}-{2}".format(self.deDateEdit.date().year(), self.deDateEdit.date().month(), self.deDateEdit.date().day()) #QDateEdit
self.LineEdit.setText(Date_2021_03_26) #QLineEdit
def main():
app = QApplication(sys.argv)
win = TEST()
win.show()
app.exec_()
if __name__ == '__main__':
main()

you will notice that when you try to type this specific date: 2021-03-26 by keyboard, it will be rejected (in my case it sat to 2021-03-01)
if you try other dates (try change the year, month and day) it will accept them. why this happened only for this date 2021-03-26?
note that: if you use the updown arrows to type(select) the date, it will accept the date 2021-03-26
I hope someone help me to understand the reason.

ChrisW67
12th April 2021, 09:38
No issue here with Qt 5.12 and Python 3.8.5. Break for you?


from PyQt5.QtWidgets import *
from PyQt5.QtCore import QDate
import sys

class Widget(QWidget):
def __init__(self):
QWidget.__init__(self)
self.dateEdit = QDateEdit()
self.dateEdit.setDate(QDate(2021, 3, 1))
self.dateEdit.setDisplayFormat("yyyy-MM-dd")
self.lineEdit = QLineEdit(self)
layout = QVBoxLayout(self)
layout.addWidget(self.dateEdit)
layout.addWidget(self.lineEdit)
self.dateEdit.setFocus()
self.dateEdit.dateChanged.connect(self.showDate)

def showDate(self, date):
dateString = "{0}-{1}-{2}".format(date.year(), date.month(), date.day())
self.lineEdit.setText(dateString)

def main():
app = QApplication(sys.argv)
win = Widget()
win.show()
app.exec_()

if __name__ == '__main__':
main()

iAl
12th April 2021, 16:16
my friend,, I tried your code.
if you changed self.dateEdit.setDate(QDate(2021, 3, 1)) to self.dateEdit.setDate(QDate(2021, 1, 1))
and tried to type 2021-03-26 , you will get a problem (bug)
I can't set date to 2021-03-01, because when you design an application, you will set the date to the default which is year/01/01 .