Search code examples
c++signals-slots

QCalendarWidget: how to set specific day when changing the month?


I created a QCalendarWidget in my Gui. By default, if you change the month in the calendar NavigationBar, the day is not set to anything (i.e.: the user have to select it manually otherwise no day is set). I am trying to connect a signal that each time I change the month (i.e.: from June to May) the day is set from the current day to the first of that month (i.e.: from 12 to 1).

I already built the "connect function" using sinal: currentPageChanged(int, int) which is working fine. then, inside this slot, I am trying to update the day to become the 1st which is something I could do as there is no setDay() function !!

so, how can I set the 1st day of the selected month each time I change the current month in QCalendarWidget ?


Solution

  • try inside your slot (assuming your QCalendarWidget is named calendar):

    QDate date = calendar.selectedDate();
    date.setDate(date.year(),date.month(),1);
    calendar.setSelectedDate(date);