Calendar widget should hide when clicking somewhere else

This patch adds a event filter to hide the calendar
widget when it loses focus

Signed-off-by: Tomaz Canabrava <tomaz.canabrava@intel.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Tomaz Canabrava 2014-06-30 18:20:17 -03:00 committed by Dirk Hohndel
parent 927bc5d601
commit facf6e4b59
2 changed files with 15 additions and 2 deletions

View file

@ -297,8 +297,7 @@ DateWidget::DateWidget(QWidget *parent) : QWidget(parent),
setDate(QDate::currentDate()); setDate(QDate::currentDate());
setMinimumSize(QSize(80,64)); setMinimumSize(QSize(80,64));
setFocusPolicy(Qt::StrongFocus); setFocusPolicy(Qt::StrongFocus);
calendarWidget->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint); calendarWidget->setWindowFlags(Qt::FramelessWindowHint);
calendarWidget->setWindowModality(Qt::ApplicationModal);
calendarWidget->setFirstDayOfWeek(getLocale().firstDayOfWeek()); calendarWidget->setFirstDayOfWeek(getLocale().firstDayOfWeek());
calendarWidget->setVerticalHeaderFormat(QCalendarWidget::NoVerticalHeader); calendarWidget->setVerticalHeaderFormat(QCalendarWidget::NoVerticalHeader);
@ -306,8 +305,19 @@ DateWidget::DateWidget(QWidget *parent) : QWidget(parent),
connect(calendarWidget, SIGNAL(clicked(QDate)), calendarWidget, SLOT(hide())); connect(calendarWidget, SIGNAL(clicked(QDate)), calendarWidget, SLOT(hide()));
connect(calendarWidget, SIGNAL(activated(QDate)), this, SLOT(setDate(QDate))); connect(calendarWidget, SIGNAL(activated(QDate)), this, SLOT(setDate(QDate)));
connect(calendarWidget, SIGNAL(clicked(QDate)), this, SLOT(setDate(QDate))); connect(calendarWidget, SIGNAL(clicked(QDate)), this, SLOT(setDate(QDate)));
calendarWidget->installEventFilter(this);
} }
bool DateWidget::eventFilter(QObject *object, QEvent *event)
{
if(event->type() == QEvent::FocusOut){
calendarWidget->hide();
return true;
}
return QObject::eventFilter(object, event);
}
void DateWidget::setDate(const QDate& date) void DateWidget::setDate(const QDate& date)
{ {
mDate = date; mDate = date;
@ -367,6 +377,8 @@ void DateWidget::mousePressEvent(QMouseEvent *event)
{ {
calendarWidget->move(event->globalPos()); calendarWidget->move(event->globalPos());
calendarWidget->show(); calendarWidget->show();
calendarWidget->raise();
calendarWidget->setFocus();
} }
void DateWidget::focusInEvent(QFocusEvent *event) void DateWidget::focusInEvent(QFocusEvent *event)

View file

@ -104,6 +104,7 @@ protected:
void focusOutEvent(QFocusEvent *); void focusOutEvent(QFocusEvent *);
void keyPressEvent(QKeyEvent *); void keyPressEvent(QKeyEvent *);
void changeEvent(QEvent *); void changeEvent(QEvent *);
bool eventFilter(QObject *, QEvent *);
signals: signals:
void dateChanged(const QDate& date); void dateChanged(const QDate& date);
private: private: