[Solved]Activating calender popup on focus in event

  • I have a requirement in which I want to activate the calender popup using the keyboard. But the problem is that, in the focus in event the popup doesn't work.

    Please let me know if I need to change the approach or add something more that I am missing.

  • Moderators

    see my DocNote on the "QTimeEdit":http://qt-project.org/doc/qt-4.8/qdatetimeedit.html. You could do something like that in your focus event handler method:

    void MyDateEdit::focusInEvent ( QFocusEvent * event )
    QStyleOptionComboBox opt;
    QRect rect = this->style()->subControlRect(QStyle::CC_ComboBox,&opt,QStyle::SC_ComboBoxArrow);

     QMouseEvent event(QEvent::MouseButtonPress, rect.center(), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
     QApplication::sendEvent(this, &event);
     QMouseEvent event(QEvent::MouseButtonRelease, rect.center(), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
     QApplication::sendEvent(this, &event);


    this should open the calendar popup on focus in.

  • Since dateEdit inherits QAbstractSipnBox: you need to make few modification to the above code,

     @QStyleOptionSpinBox opt;


    QRect rect = this->style()->subControlRect(QStyle::CC_SpinBox,&opt,QStyle::SC_SpinBoxDown);@

  • Moderators

    no, because when the calendar popup is enabled the time edit looks more like a combobox than a spinbox right?
    And thats even the code QDateTimeEdit uses itself to paint the calendar button internally btw.

  • But the code didnt work with QStyleOptionComboBox.

  • Moderators

    yes you are right.. .sorry.
    It over-hasted when i looked it up from some old code of mine.

  • It happens no worries :)

  • @raven-worx I need to do something similar but I do not want to subclass QDateEdit and use focusInEvent(), I use eventFilter in my MainWindow class. Is it possible to use your code?

Log in to reply