1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- #include "calendardtedit.h"
- #include <QDebug>
- #include <QApplication>
- #include <QDesktopWidget>
- #include <QKeyEvent>
- #include <QEvent>
- #include "calendarex.h"
- CalendarDTEdit::CalendarDTEdit(QWidget *parent) : QDateTimeEdit(parent)
- {
- setCalendarPopup(false);// 阻止自带的日历窗口
- }
- CalendarDTEdit::~CalendarDTEdit()
- {
- }
- void CalendarDTEdit::CloseCalendar()
- {
- }
- /* 手动触发日期选择弹框 */
- void CalendarDTEdit::triggerCalendarPopup()
- {
- }
- /**
- * @brief 设置手动禁止修改日期区域,只能使用弹窗
- *
- * @param value 是否禁止修改日期区域
- * @param triggerPopup 点击日期区域是否会跳出弹窗
- */
- void CalendarDTEdit::setManualDisableEdit(bool value, bool triggerPopup)
- {
- if(value)
- {
- setReadOnly(true);
- for(auto& it : children())
- {
- if(it->objectName() == "qt_spinbox_lineedit")
- {
- m_pCalendar = it;
- m_pCalendar->installEventFilter(this);
- }
- }
- }else {
- setReadOnly(false);
- if(m_pCalendar != nullptr)
- {
- m_pCalendar->removeEventFilter(this);
- m_pCalendar = nullptr;
- }
- }
- m_isPopup = triggerPopup;
- }
- void CalendarDTEdit::mousePressEvent(QMouseEvent *e)
- {
- Q_UNUSED(e);
- CalendarEx *pC = new CalendarEx(date());
- if (nullptr == pC) return;
- connect(pC, &CalendarEx::sig_DateChanged, this, [this](const QDate& date){
- if (date.isValid()) {
- setDate(date);
- }
- });
- pC->show();
- pC->positionCalendarPopup(this);
- }
- bool CalendarDTEdit::eventFilter(QObject* watched, QEvent* event)
- {
- if(watched == m_pCalendar)
- {
- if(event->type() == QEvent::MouseButtonPress)
- {
- // qDebug() << "点击了日期区域";
- if(m_isPopup)
- {
- mousePressEvent(nullptr);
- }
- }
- }
- return QDateTimeEdit::eventFilter(watched, event);
- }
|