PDA

View Full Version : How to put empty value in QDateEdit?



sawerset
28th November 2008, 15:17
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?

aamer4yu
28th November 2008, 18:15
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()).

kghose
28th November 2008, 19:21
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/viewvc/chhobi/ChhobiQT/metadata_edit.cpp?revision=1.20&view=markup

line 51

darkadept
6th March 2009, 18:40
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:


#include <QDateEdit>
class NullDateEdit : public QDateEdit
{
Q_OBJECT
Q_PROPERTY(QDate nullDate READ nullDate WRITE setDate USER true)
public:
NullDateEdit(const QDate& date, QWidget* parent);
NullDateEdit(QWidget* parent);
~NullDateEdit();

QDate nullDate() const;

public slots:
void clear();
void setDate(const QDate& date);
};


nulldateedit.cpp is:


#include "nulldateedit.h"

NullDateEdit::NullDateEdit(const QDate& date, QWidget* parent)
: QDateEdit(date, parent)
{
this->setSpecialValueText("Null");
}

NullDateEdit::NullDateEdit(QWidget* parent)
: QDateEdit(parent)
{
this->setSpecialValueText("Null");
}

NullDateEdit::~NullDateEdit()
{
}

void NullDateEdit::clear()
{
this->setDate(this->minimumDate());
}

QDate NullDateEdit::nullDate() const
{
if (date() == this->minimumDate())
return QDate();
return date();
}

void NullDateEdit::setDate(const QDate & date)
{
if (date.isNull())
QDateEdit::setDate(this->minimumDate());
QDateEdit::setDate(date);
}

Henry Blue Heeler
8th September 2015, 23:40
Yes you can display SpecialValueText but then cannot edit the NullDateEdit field.

San_Cou
14th October 2019, 22:06
The SpecialValueText just works if you set the date equals to the minimumDate. And you need to put some char.