| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 | #include "cdate.h"#include <QHBoxLayout>#include <QPainter>#include <QEvent>#include <QDebug>#include <QMouseEvent>#include <QEventLoop>#include "calendarwidgetex.h"#include "oneshadow.h"CDate::CDate(const QDate& defaultDate,QWidget *parent,PopupType type) :    QWidget{parent},    m_parent(parent),    m_type(type){    init(defaultDate);}CDate::CDate(QWidget *parent,PopupType type) :    QWidget{parent},    m_parent(parent),    m_type(type){//    if(type == Popup)//    {//        setWindowFlags(Qt::FramelessWindowHint | Qt::Popup);//    }    init(QDate::currentDate());}/* 阻塞执行 */QDate CDate::execShow(){    this->show();    QEventLoop loop;    connect(this, &CDate::signal_close, &loop, &QEventLoop::quit);    loop.exec();    return m_date;}/* 设置日期 */void CDate::setDate(const QDate& date){    m_date = date;    m_calendarEx->setSelectedDate(date);}void CDate::paintEvent(QPaintEvent *event){    QPainter painter(this);    painter.setRenderHint(QPainter::Antialiasing);    painter.drawImage(QPoint(0,0),m_shadow->image());}/** * @brief 在这里实现点击空白处隐藏或者关闭自身 * @param watched * @param event * @return */bool CDate::eventFilter(QObject *watched, QEvent *event){    auto me = dynamic_cast<QMouseEvent*>(event);    if(nullptr != me)    {        if(me->type() == QEvent::MouseButtonPress)        {            if(m_type == Popup)            {                /* 先将自身区域和鼠标坐标都转换成全局坐标 */                QPoint gTopLeft = this->mapToGlobal(this->rect().topLeft());                QRect gRect(gTopLeft.x(),gTopLeft.y(),this->width(),this->height());                /* 判断此时的鼠标坐标是否在这个控件中 */                if(!gRect.contains(me->globalPos()))                {//                    qDebug() << QTime::currentTime() << "CDate关闭了";                    emit signal_DateChanged(m_date);                    /* 发送信号,关闭自身 */                    emit signal_close();                    this->close();                }            }        }    }    return QWidget::eventFilter(watched,event);}void CDate::init(const QDate &defaultDate){    /* 设置无边框 */    setWindowFlags(Qt::FramelessWindowHint);    /* 设置底层样式表,让最底层的透明 */    this->setAttribute(Qt::WA_TranslucentBackground);    /* 将父类的事件注册给自己 */    if(m_type == Popup && m_parent != nullptr)    {        m_parent->installEventFilter(this);    }    /* 设置布局,通过设置layout的Margin距离显示阴影 */    QHBoxLayout* layout = new QHBoxLayout(this);    this->setLayout(layout);    layout->setMargin(RADIUS);    this->resize(CALENDAR_WIDTH + RADIUS*2,CALENDAR_HEIGHT + RADIUS*2);    /* 设置日历组件 */    m_calendarEx = new CalendarWidgetEx(this);    m_calendarEx->resize(CALENDAR_WIDTH,CALENDAR_HEIGHT);    m_calendarEx->setCurrentPage(defaultDate.year(),defaultDate.month());    m_calendarEx->setSelectedDate(defaultDate);    layout->addWidget(m_calendarEx);    /* 设置自身时间 */    m_date = defaultDate;    /* 设置阴影 */    m_shadow = new OneShadow(QSize(m_calendarEx->width(),m_calendarEx->height()),RADIUS);    /* 信号和槽 */    connect(m_calendarEx,&CalendarWidgetEx::clicked,this,[this](const QDate& date){        m_date = date;        emit signal_DateChanged(date);        emit signal_close();        this->close();    });}
 |