Important: Please read the Qt Code of Conduct -

QDateEdit (QAbstractSpinBox) blank/empty value problem

  • I seem to have a real problem with getting an "empty"/"blank" value into a QDateEdit.

    As per, I have been trying to move a system which uses a plain QLineEdit for entering its dates to having a date control. Originally I had been writing my own widget for this, but abandoned once I discovered QDateEdit and QDateEdit::setCalendarPopup(true), which seemed to be perfect.

    I have now changed all code over to using QDateEdits. But belatedly I have discovered this makes the interface impossible to use because of its lack of support for "not filled in". Note that this will apply to QAbstractSpinBox in general, e.g. for QSpinBox too.

    There are plenty of places where filling in the date selector widget is not mandatory: for example, if it's for a database field the field allows NULL, or if it's one of a number of "search filters" the querier does not want to filter by date among the other criteria. Also, even if it is mandatory there may be no "default date" desired when the widget is displayed initially.

    When using a QLineEdit there has never been a problem: it is simply left blank/empty. (If required, validators can ensure it is filled in after user interaction.)

    This does not appear to be possible for a QLineEdit (or indeed any QAbstractSpinBox)? Is that indeed the case, because if it is I am in a terrible pickle after having changed all the source code to use it when it does not allow this... :(

  • Lifetime Qt Champion

    It has to have a valid date so it cannot be blank.
    only have a default date.
    You can subclass and make it work/allow invalid/blank date.

  • @mrjj
    Firstly, thank you for your prompt and relevant response.

    Disappointing that this was an issue raised in 2008, and agreed as a problem by others, with no change in 10 years....

    I have looked at the code solution there. So far as I can see it effectively uses nothing other than the existing setSpecialValueText() functionality. This has two obvious flaws:

    • It says "This will show the text "Null" in the date edit box." That's not "blank/empty". Unfortunately there is no chance my users will understand that. I doubt that another word would be acceptable either.

    • Even if I can get over the first issue, the post says nothing about how the user can choose to empty out the value in the date. This is a show-stopper. If, for whatever reason, the widget has a date in it --- perhaps an existing value from a database, or perhaps the user started by picking a date --- how does he revert to it being blank?? How does he pick "null"/"empty"? My understanding is: the explanation there includes: "[for Null date] The date value will be Sept 14, 1752.". So the user needs to pick 14/09/1752 (or similar) from the calendar picker as the only way (e.g. cannot even type "Null") to revert to "no date". Seriously??

  • Lifetime Qt Champion

    I agree, that QDateEdit could really use a placeHolderText :)
    But it comes from the fact that the actual datatime class demands valid date and
    that is pretty ok. sadly the Widget was never changed to be more happy for invalid dates. or unset date.

    If you need to be able to "Reset" to unset, you must program that too. Its not included.
    Its not a full prefect solution. just code snippet. you need to make it perfect for you. :)

    In my design, i did something else.
    I had a checkbox if the dates should be used at all.
    So sometimes other design can help, but you sound like you need
    a DateEdit that has the concept of set/unset and hence you must add that too.

    ps. there might be completely other options i missed :)

  • @mrjj
    Unfortunately, you seem to be confirming my understanding of the lack of support for empty dates/numbers/etc. in all the QAbstractSpinBox-derived classes :(

    The same sort of issue arises in, say, MS Windows Forms NumericUpDown. But there you can work around it, e.g. . In particular, the user can empty out the text, and you can recognise that and set the control to have the "Null" value. With this QDateEdit I can't do that, because the editor does not allow me to delete all the characters :(

    I agree this leaves us with something like adding an extra control for the user to click to do the emptying, as you have done with your checkbox, which I had thought of. Unfortunately I am faced with dumb but pernickety users, who will doubtless riot over this... :(

    The really irritating thing is: I had starting designing my own "composite widget" consisting of a normal QLineEdit, plus a "..." QPushButton which led to a modal dialog holding the QCalendarWidget which passes the date in the line edit back & forth. Because I was using a normal QLineEdit there was no problem with "blank" value. But when I discovered QDateEdit::setCalendarPopup() I presumed this would be better and scrapped my own code... :(

  • Lifetime Qt Champion

    Well, as far as i know, its due to this class
    it was not really cooperative in being "empty"

    Maybe its possible and i never saw the light but it seems to come up from time to time so
    at least its a secret :)

    Did you completely deleted your own code ?
    that is annoying, yes:)

Log in to reply