Nominate our 2022 Qt Champions!

QDateTimeEdit with date *and* time picker

  • Hi,

    I want to create a DateTimeEdit with a popup that contains both, a date picker and a time picker (as shown in the image below or in this live example).
    alt text

    I have read the docs of QDateTimeEdit and QCalendarWidget and also read parts of QDateTimeEdit's source code, but I am still not sure what would be the best approach to implement this (in PyQt5, but I guess whether I do it in Python or C++ is not too important for the general approach).

    The internal QCalendarPopup class already seems to have a layout component that could, in theory, contain a calendar widget as well as a "TimeSelectionWidget". However, I have no idea how to extend it in a clean way.


  • Moderators

    i haven't tried it but it should be possible:

    QDateTimeEdit* dt = new QDateTimeEdit;
    QWidget* popup = dt->calendarWidget()->window();
    QListView* timeList = new QListView;
    //init timeList widget
    QWidget* container = new QWidget;
    // set the layout and add the "calendar widget" and "timeList" widgets (and buttosn) to your needs
    QVBoxLayout *popupLayout = qobject_cast<QVBoxLayout*>(popup->layout());
    QLayoutItem *item;
     while( (item = popupLayout ->takeAt(0)) )
             delete item;
    popupLayout->addWidget( container );

    Also you need to make sure to select the time (in the time list view) whenever the datetime changes.

  • @raven-worx Thank you very much!

    I already got a minimal example working. I'll post a complete example once everything works well. :)

  • I made some progress and the new widgets looks quite good (not perfect, though):

    The problem with resizing and spacing everything correclty is, that I can't access all of the calendar widget's privates stuff.

    This also leads to problems with signals/slots. By default, the calendar popup sets the DateTimeEdit's date and closes, if I click on the month calendar. However, I only want to to set the Date/Time and close the Window when I click on the Done button, so that I can select a Date, an Hour and Minute. Therefore, I need to disconnect the original signals, but they are not accessible, too.

    I don't really want to write my own CalendarWidget-clone just to do these minor modifications. What else could I do?

  • Lifetime Qt Champion

    Did you try implementing mousePress and not call base class?
    You can maybe eat the mouse event that triggers the signal to be sent and
    that way remove the popup.

  • Moderators

    @Stefan-Scherfke said in QDateTimeEdit with date *and* time picker:

    What else could I do?


    1. install an event Filter on the popup widget (window) and intercept the CloseEvent and call ignore() on it and return true in the event-filter
    2. call blockSignals(true) on all widgets which shouldn't trigger signals