0

I'm trying to catch mouse click on QDateEdit widget by handling QEvent::MouseButtonRelease event, but can't find a way to do it. I tried to override QWidget::event method of the parent widget, but it seems that events go through children to parent, and QDateEdit internally handles those events without propagating to a parent. Is there any correct solution or workaround?

Denis
  • 2,786
  • 1
  • 14
  • 29
  • 3
    Why do you need to catch mouse click? There is a bunch of methods to detect changes in a QDateTimeEdit and, maybe, handling mouse events are not the best solution. – Dimitry Ernot Apr 04 '19 at 08:32

2 Answers2

2

QDateEdit extends a QWidget class. So you can just inherit QDateEdit and override virtual void mouseReleaseEvent(QMouseEvent *event) function and do what you want there.

Update:

Function mouseReleaseEvent is really not invoke.

Try to install an event filter to the line edit in QDateEdit. Example:

MyDateEdit.h

#include <QDateEdit>

class MyDateEdit : public QDateEdit
{
  Q_OBJECT
public:
  MyDateEdit(QWidget *parent = 0);
  bool eventFilter(QObject* object, QEvent* event) override;
};

MyDateEdit.cpp

#include "MyDateEdit.h"    
#include <QDebug>
#include <QEvent>
#include <QLineEdit>

MyDateEdit::MyDateEdit(QWidget *parent) : QDateEdit (parent)
{
  installEventFilter(this);
  lineEdit()->installEventFilter(this);
}

bool MyDateEdit::eventFilter(QObject* object, QEvent* event)
{
  if (object == this || object == lineEdit())
  {
    if (event->type() == QEvent::MouseButtonRelease)
    {
      qDebug() << "Mouse release event";
    }
  }    
  return QDateEdit::eventFilter(object, event);
}
  • I tried to do that, but QDateEdit never receive mouseReleaseEvent. It is probably because the underlying QLineEdit widget doesn't propagate it to the parent – Denis Apr 04 '19 at 09:32
  • Thanks, lineEdit() method is just what I need. I haven't noticed this useful method in QAbstractSpinBox – Denis Apr 04 '19 at 15:20
1

One way to do this is to install an eventFilter. The eventFilter section of the Qt documentation provides an example of how it is use.

Your window class should override eventFilter

bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
  if (obj == dateEdit) {
    if (event->type() == QEvent::MouseButtonPress) {
        // do what you want to do
        // alternatively use QEvent::MouseButtonRelease 
        return true;
    } else {
        return false;
    }
  } else {
    // pass the event on to the parent class
    return QMainWindow::eventFilter(obj, event);
  }
}

In your window constructor, install the filter on the actual widget:

dateEdit->installEventFilter(this);
zeFrenchy
  • 6,541
  • 1
  • 27
  • 36
  • I tried it, but QEvent::MouseButtonPress event coming to eventFilter only when I press to a drop-down arrow of a QDateEdit widget – Denis Apr 04 '19 at 11:02
  • True enough. I guess it makes sense to have the widget swallow those since you presumably want it to handle the user interaction in order to get a date. So, the question is what exactly are you wanting to do upon mouse button release? – zeFrenchy Apr 04 '19 at 13:26
  • The goal was a logging all user interactions, including clicks on date widgets, even when those widgets already have a focus, i.e. focus events is not that I just need – Denis Apr 04 '19 at 15:24