QDateEdit 设置为空,显示日历

在项目里偶尔需要用到QDateEdit设置为空,但是QDateEdit并不支持,继承QDateEdit实现,效果:

1.设置为空:

#ifndef CDATEEDIT_H
#define CDATEEDIT_H

#include <QtCore>
#include <QWidget>
#include <QDateEdit>
#include <QStyle>
#include <QPushButton>
#include <QLineEdit>
#include <QStyleOptionSpinBox>
#include <QKeyEvent>
#include <QCalendarWidget>
class CDateEdit : public QDateEdit
{

    Q_OBJECT

public:
    explicit CDateEdit(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(CDateEdit)
    class Private;
    friend class Private;
    Private* d;

};

#endif // CDATEEDIT_H
#include "CDateEdit.h"

class CDateEdit::Private {
public:
    Private(CDateEdit* qq) : q(qq), null(false), nullable(false) {}

    CDateEdit* const q;

    bool null;
    bool nullable;

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

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


QDateTime CDateEdit::dateTime() const
{
    if (d->nullable && d->null) {
        return QDateTime();
    }
    else {
        return QDateEdit::dateTime();
    }
}


QDate CDateEdit::date() const
{
    if (d->nullable && d->null) {
        return QDate();
    }
    else {
        return QDateEdit::date();
    }
}


QTime CDateEdit::time() const
{
    if (d->nullable && d->null) {
        return QTime();
    }
    else {
        return QDateEdit::time();
    }
}


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


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

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

bool CDateEdit::isNullable() const
{
    return d->nullable;
}

void CDateEdit::setNullable(bool enable)
{
    d->nullable = enable;

    update();
}


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

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

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

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

void CDateEdit::paintEvent(QPaintEvent *event)
{
    d->setNull(d->null);
    QDateEdit::paintEvent(event);

}

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

    QDateEdit::keyPressEvent(event);
}

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

bool CDateEdit::focusNextPrevChild(bool next)
{
    if (d->nullable && d->null){
        return QAbstractSpinBox::focusNextPrevChild(next);
    }
    else {
        return QDateEdit::focusNextPrevChild(next);
    }
}

QValidator::State CDateEdit::validate(QString &input, int &pos) const
{
    if (d->nullable && d->null){
        return QValidator::Acceptable;
    }
    return QDateEdit::validate(input, pos);
}

2.弹出日历

void    setCalendarPopup(bool enable) //弹出日历

3.样式设置

QDateEdit
{
    border: 1px solid #dbdbdb;
    color: #666666;
    font: 14px "微软雅黑";
}

QDateEdit:on { /* shift the text when the popup opens */
    padding-top: 3px;
    padding-left: 4px;
}

QDateEdit::drop-down {
    subcontrol-origin: padding;
    subcontrol-position: top right;
    width: 40px;

    border-left-color: darkgray;
    border-left-style: solid; /* just a single line */
    border-top-right-radius: 3px; /* same radius as the QComboBox */
    border-bottom-right-radius: 3px;
}

QDateEdit::down-arrow {
    image: url(:/style/blue/xiaoxi/xiaoxi_icon_calendar_n.png);
}

QDateEdit::down-arrow:on { /* shift the arrow when popup is open */
    top: 1px;
    left: 1px;
}

转载请保存原文出处,谢谢!

posted @ 2017-10-18 09:57  积跬步,到千里  阅读(8087)  评论(1编辑  收藏  举报