Results 1 to 6 of 6

Thread: How to put empty value in QDateEdit?

  1. #1
    Join Date
    Aug 2008
    Posts
    28
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to put empty value in QDateEdit?

    I use QDateEdit to display some data from data base.
    If the value of field is empty then the 1.1.2000 is shown in QDateEdit.
    1 - How QDateEdit can be placed to the empty value?
    2 - How user can put QDateEdit to empty value?

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to put empty value in QDateEdit?

    From the docs -
    This property holds the QDate that is shown in the widget.

    By default, this property contains a date referring to January 1, 2000.
    Try setting the date manually... like setDate(QDate()).

  3. #3
    Join Date
    Jul 2008
    Location
    East Coast, USA
    Posts
    40
    Thanks
    6
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Unhappy Re: How to put empty value in QDateEdit?

    I think I know what you are after and it can't be done - QDateEdit can't store the null date and display a blank or somesuch to indicate that. I had an issue with this and my ugly hack was to cover up the qdateedit with a qpushbutton when the QDate was a null. The user could hit the pushbutton to set a date.

    http://chhobi.cvs.sourceforge.net/vi...20&view=markup

    line 51

  4. #4
    Join Date
    May 2006
    Posts
    70
    Thanks
    12
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to put empty value in QDateEdit?

    What I did was subclass QDateEdit and overrode some functions.

    I reimplemented QAbstractSpinBox->clear() to set the date to the minimumDate. This will show the text "Null" in the date edit box. The date value will be Sept 14, 1752.
    I implemented the nullDate() method to return a null date when the actual value is the same as minimumDate.
    I overrode QDateTimeEdit::setDate() method to set the date to the minimumDate if trying to set a null value.
    Then I created a new user property the uses my nullDate() instead of date() and uses my overridden setDate().

    If you use the following class in code you need to read nullDate() instead of using just date() but if you use the USER property you don't have to worry about this because it automatically reads nullDate(). If you accidentally use date() you will get the value 09/14/1752 instead of null.

    nulldateedit.h is:
    Qt Code:
    1. #include <QDateEdit>
    2. class NullDateEdit : public QDateEdit
    3. {
    4. Q_OBJECT
    5. Q_PROPERTY(QDate nullDate READ nullDate WRITE setDate USER true)
    6. public:
    7. NullDateEdit(const QDate& date, QWidget* parent);
    8. NullDateEdit(QWidget* parent);
    9. ~NullDateEdit();
    10.  
    11. QDate nullDate() const;
    12.  
    13. public slots:
    14. void clear();
    15. void setDate(const QDate& date);
    16. };
    To copy to clipboard, switch view to plain text mode 

    nulldateedit.cpp is:
    Qt Code:
    1. #include "nulldateedit.h"
    2.  
    3. NullDateEdit::NullDateEdit(const QDate& date, QWidget* parent)
    4. : QDateEdit(date, parent)
    5. {
    6. this->setSpecialValueText("Null");
    7. }
    8.  
    9. NullDateEdit::NullDateEdit(QWidget* parent)
    10. : QDateEdit(parent)
    11. {
    12. this->setSpecialValueText("Null");
    13. }
    14.  
    15. NullDateEdit::~NullDateEdit()
    16. {
    17. }
    18.  
    19. void NullDateEdit::clear()
    20. {
    21. this->setDate(this->minimumDate());
    22. }
    23.  
    24. QDate NullDateEdit::nullDate() const
    25. {
    26. if (date() == this->minimumDate())
    27. return QDate();
    28. return date();
    29. }
    30.  
    31. void NullDateEdit::setDate(const QDate & date)
    32. {
    33. if (date.isNull())
    34. QDateEdit::setDate(this->minimumDate());
    35. QDateEdit::setDate(date);
    36. }
    To copy to clipboard, switch view to plain text mode 

  5. The following 3 users say thank you to darkadept for this useful post:

    droetker (13th June 2012), Netwiz (5th November 2009), Walter (18th August 2009)

  6. #5
    Join Date
    Dec 2013
    Location
    Colorado
    Posts
    45
    Thanks
    15
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: How to put empty value in QDateEdit?

    Yes you can display SpecialValueText but then cannot edit the NullDateEdit field.

  7. #6
    Join Date
    Oct 2012
    Posts
    13
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to put empty value in QDateEdit?

    The SpecialValueText just works if you set the date equals to the minimumDate. And you need to put some char.

Similar Threads

  1. clear text in QDateEdit
    By dyams in forum Qt Programming
    Replies: 6
    Last Post: 31st August 2010, 23:30
  2. QSqlTableModel inserts empty rows
    By Nesbitt in forum Qt Programming
    Replies: 2
    Last Post: 6th August 2008, 13:47
  3. QDateEdit and empty date
    By vieraci in forum Qt Programming
    Replies: 1
    Last Post: 3rd November 2007, 17:19
  4. remove directory empty or not empty
    By raphaelf in forum Newbie
    Replies: 12
    Last Post: 27th October 2006, 08:30
  5. Problem with receiving events from QDateEdit
    By gunhelstr in forum Qt Programming
    Replies: 4
    Last Post: 20th April 2006, 12:21

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.