4
votes

I am creating a GUI and trying to get a date from the user using DateEdit. It works fine, except that when I start the application, the default date is 1/1/2000. How do I get the DateEdit widget to default to system time? Nothing I've found in the PyQt docs has helped me.

self.date = QtGui.QDateEdit(self.wizardPage2)
self.date.setMaximumDateTime(QtCore.QDateTime(QtCore.QDate(7999, 12, 28), QtCore.QTime(23, 59, 59)))
self.date.setCalendarPopup(True)
1

1 Answers

8
votes

You are looking for QDateTime.currentDateTime():

#!/usr/bin/env python
#-*- coding:utf-8 -*-

from PyQt4 import QtGui, QtCore

class MyWindow(QtGui.QWidget):
    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)

        self.dateEdit = QtGui.QDateEdit(self)
        self.dateEdit.setDateTime(QtCore.QDateTime.currentDateTime())
        self.dateEdit.setMaximumDate(QtCore.QDate(7999, 12, 28))
        self.dateEdit.setMaximumTime(QtCore.QTime(23, 59, 59))
        self.dateEdit.setCalendarPopup(True)

        self.layoutHorizontal = QtGui.QHBoxLayout(self)
        self.layoutHorizontal.addWidget(self.dateEdit)

if __name__ == "__main__":
    import sys

    app = QtGui.QApplication(sys.argv)
    app.setApplicationName('MyWindow')

    main = MyWindow()
    main.show()

    sys.exit(app.exec_())