Qt World Summit: Register Today!

Set current date in QCalendarWidget

  • Hi, hello.

    I am using a QCalendarWidget for date input that is stored in a QLineEdit. When I double click the QLineEdit box it pops up a QCalendarWidget for date selection. I wish to set the current date that is already displayed in the QLineEdit. I have found this in the Docs.

    selectedDate : QDate
    This property holds the currently selected date.
    The selected date must be within the date range specified by the minimumDate and maximumDate properties. By default, the selected date is the current date.
    Access functions:
    QDate  selectedDate() const
    void setSelectedDate(const QDate &date)

    However, setSelectedDate does not appear to be a valid function of QCalendarWidget.

    What am I missing here?



  • You need to pass a QDate to that Method. In your QLineEdit which format has your Date? Maybe you need to set QDate with QDate::fromString(string, format) an pass it to QCalendarWidget

  • Not sure I follow you, can you provide example?


  • Lifetime Qt Champion


    Out of curiosity, why note use a QDateEdit ?

  • Hi,

    Original design was user inputted a date in form of dd/mm/yyyy, then later on I changed it to a popup style calendar. Which works great, I just want to have the calendar start off on the date that's in the box instead of current date.


  • Then pass the Date in the QLineEdit to the QCalendarWidget wit setSelectedDate() or show the Error you get.

  • Hi Guys, and thanks for the help.

    The issue was setCurrentDate() was not a valid function. The problem was, I set my QCalendarWidget class as private within the popup class. Once I wrote a function within the popup class to modify the current date all works as expected.

    Thanks. again.

Log in to reply