qt 自定义可删除的QDateEdit控件

目录

1.引言

2.头文件

3.源文件

4.调用方法


1.引言

实际开发中,关于日期的设定都是通过QDateEdit控件设置的,但是该控件默认会有一个初试时间设定,无法手动删除。这就需要我们自行实现这个功能,以下就是全部的代码了,供大家参考使用:

2.头文件

class CUIDateEdit : public QDateEdit
{
    Q_OBJECT
public:
    explicit CUIDateEdit(QWidget *parent = 0);

    QDateTime dateTime() const;
    QDate date() const;
    QTime time() const;

    bool isNullable() const;
    void setNullable(bool enable);

    QSize sizeHint() const;
    QSize minimumSizeHint() const;

protected:
    void showEvent(QShowEvent *event);
    void resizeEvent(QResizeEvent *event);
    void paintEvent(QPaintEvent *event);
    void keyPressEvent(QKeyEvent *event);
    void mousePressEvent(QMouseEvent *event);
    bool focusNextPrevChild(bool next);
    QValidator::State validate(QString &input, int &pos) const;

public slots:
    void setDateTime(const QDateTime &dateTime);
    void setDate(const QDate &date);
    void setTime(const QTime &time);

private:
    Q_DISABLE_COPY(CUIDateEdit)
    class Private;
    friend class Private;
    Private* d;
};

3.源文件

#include "CUIDateEdit.h"
#include 
#include 
#include 
#include 
#include 
#include 
#include 

class CUIDateEdit::Private
{
public:
    Private(CUIDateEdit* qq) : q(qq), bNull(false), bNullable(false) {}

    CUIDateEdit* const q;

    bool bNull;
    bool bNullable;

    void setNull(bool n)
    {
        bNull = n;
        if (bNull)
        {
            QLineEdit *edit = q->findChild("qt_spinbox_lineedit");
            if (!edit->text().isEmpty())
            {
                edit->clear();
            }
        }
    }
};

CUIDateEdit::CUIDateEdit(QWidget *parent) : QDateEdit(parent), d(new Private(this))
{
}

QDateTime CUIDateEdit::dateTime() const
{
    if (d->bNullable && d->bNull)
    {
        return QDateTime();
    }
    else
    {
        return QDateEdit::dateTime();
    }
}


QDate CUIDateEdit::date() const
{
    if (d->bNullable && d->bNull)
    {
        return QDate();
    }
    else
    {
        return QDateEdit::date();
    }
}


QTime CUIDateEdit::time() const
{
    if (d->bNullable && d->bNull)
    {
        return QTime();
    }
    else
    {
        return QDateEdit::time();
    }
}


void CUIDateEdit::setDateTime(const QDateTime &dateTime)
{
    if (d->bNullable && !dateTime.isValid())
    {
        d->setNull(true);
    }
    else
    {
        d->setNull(false);
        QDateEdit::setDateTime(dateTime);
    }
}


void CUIDateEdit::setDate(const QDate &date)
{
    if (d->bNullable && !date.isValid())
    {
        d->setNull(true);
    }
    else
    {
        d->setNull(false);
        QDateEdit::setDate(date);
    }
}

void CUIDateEdit::setTime(const QTime &time)
{
    if (d->bNullable && !time.isValid())
    {
        d->setNull(true);
    }
    else
    {
        d->setNull(false);
        QDateEdit::setTime(time);
    }
}

bool CUIDateEdit::isNullable() const
{
    return d->bNullable;
}

void CUIDateEdit::setNullable(bool enable)
{
    d->bNullable = enable;
    update();
}


QSize CUIDateEdit::sizeHint() const
{
    const QSize sz = QDateEdit::sizeHint();
    return QSize(sz.width() + 3, sz.height());
}

QSize CUIDateEdit::minimumSizeHint() const
{
    const QSize sz = QDateEdit::minimumSizeHint();
    return QSize(sz.width() + 3, sz.height());
}

void CUIDateEdit::showEvent(QShowEvent *event)
{
    QDateEdit::showEvent(event);
    d->setNull(d->bNull);
}

void CUIDateEdit::resizeEvent(QResizeEvent *event)
{
    QDateEdit::resizeEvent(event);
}

void CUIDateEdit::paintEvent(QPaintEvent *event)
{
    d->setNull(d->bNull);
    QDateEdit::paintEvent(event);

}

void CUIDateEdit::keyPressEvent(QKeyEvent *event)
{
    if (d->bNullable && (event->key() >= Qt::Key_0) && (event->key() <= Qt::Key_9) && d->bNull)
    {
        setDateTime(QDateTime::currentDateTime());
    }
    if (event->key() == Qt::Key_Tab && d->bNullable && d->bNull)
    {
        QAbstractSpinBox::keyPressEvent(event);
        return;
    }
    if (event->key() == Qt::Key_Backspace && d->bNullable)
    {
        QLineEdit *edit = this->findChild("qt_spinbox_lineedit");
        if (edit->selectedText() == edit->text())
        {
            setDateTime(QDateTime());
            event->accept();
            return;
        }
    }

    QDateEdit::keyPressEvent(event);
}

void CUIDateEdit::mousePressEvent(QMouseEvent *event)
{
    bool saveNull = d->bNull;
    QDateEdit::mousePressEvent(event);
    if (d->bNullable && saveNull && calendarWidget()->isVisible()) {
        setDateTime(QDateTime::currentDateTime());
    }
}

bool CUIDateEdit::focusNextPrevChild(bool next)
{
    if (d->bNullable && d->bNull)
    {
        return QAbstractSpinBox::focusNextPrevChild(next);
    }
    else
    {
        return QDateEdit::focusNextPrevChild(next);
    }
}

QValidator::State CUIDateEdit::validate(QString &input, int &pos) const
{
    if (d->bNullable && d->bNull)
    {
        return QValidator::Acceptable;
    }
    return QDateEdit::validate(input, pos);
}

4.调用方法

CUIDateEdit *pDaEt = new CUIDateEdit();
pDaEt->setNullable(true);

 这样就可以使用backspace删除啦。

你可能感兴趣的:(qt,开发语言)