Search code examples
qtqcalendarwidget

QDateEdit calendar popup


I'm trying to get a QDateEdit to allow the QCalendarWidget to show when requested (rather than just on clicking the down arrow). For example, somewhere in my class I should be able to say:

ui.datepicker.showCalendar()

and it should load up the calendar that appears right below the date picker.

It looks like I need to sub-class QDateEdit, as this doesn't work:

QDateEdit *de = new QDateEdit();
de->calendarWidget()->show();

I've also tried sending keyboard commands as dictated when you go through the QDateTimeEdit.cpp source for Qt, but seems my keyboard shortcuts are disabled or something.

Any ideas on what I have to do to sub-class to get this to work? I was thinking of something like:

class MyDateEdit : QDateEdit
{
  Q_OBJECT

protected:
  void mouseEvent(QEvent *event) {
    this.calendarWidget().show();
  }
};

But alas that also doesn't seem to compile in or work correctly.


Solution

  • I was able to figure it out on my own - still no sure how to get QDateEdit to work properly, but I used a QLineEdit and it suited my needs. Just connect QCalendarWidget's "onClick(QDate)" to a slot you create that does a:

    setText(date.toString("M/d/yyyy"));
    ui->calendar->hide();
    

    Then add an event filter to the QLineEdit using the "OnFocusIn" event that does a "ui->calendar->show();" See: Get a notification/event/signal when a Qt widget gets focus