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>
{
Q_OBJECT
Q_PROPERTY(QDate nullDate READ nullDate WRITE setDate USER
true) public:
NullDateEdit
(const QDate
& date,
QWidget* parent
);
~NullDateEdit();
public slots:
void clear();
void setDate(const QDate& date);
};
#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);
};
To copy to clipboard, switch view to plain text mode
nulldateedit.cpp is:
#include "nulldateedit.h"
NullDateEdit
::NullDateEdit(const QDate
& date,
QWidget* parent
){
this->setSpecialValueText("Null");
}
NullDateEdit
::NullDateEdit(QWidget* parent
){
this->setSpecialValueText("Null");
}
NullDateEdit::~NullDateEdit()
{
}
void NullDateEdit::clear()
{
this->setDate(this->minimumDate());
}
QDate NullDateEdit
::nullDate() const {
if (date() == this->minimumDate())
return date();
}
void NullDateEdit
::setDate(const QDate & date
) {
if (date.isNull())
}
#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);
}
To copy to clipboard, switch view to plain text mode
Bookmarks