Qt: How to catch QDateEdit click event? - c++

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?

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);
}

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);

Related

Qt Transparent for selected Mouse Events

C++ Qt newbe here. I work with a QDial object that is intended to be controlled with a mouse wheel, it works fine as such, emitting valueChanged() signals when necessary.
I would like to put a semi-transparent QToolButton on top of it, allowing users to click on the button (and set QDial value to a pre-defined number) while maintaining the ability to use the mouse wheel to control QDial as usual.
I experimented a bit with the TransparentForMouseEvents attribute:
ui->toolButton_Example->setAttribute(Qt::WA_TransparentForMouseEvents);
The problem is - the above code switches off all events, including the ability to emit the clicked() signal.
Is there a way to make a QToolButton transparent selectively for MouseWheelEvents while preserving the ability to respond to MouseClick events? Or would this require rewriting the event filter from scratch?
EDIT: Just to clarify - This question is about making QToolButton transparent to MouseWheel EVENTS while still allowing it to respond to MouseClick EVENTS. It is not about making the button transparent in the graphical sense.
SOLUTION
OK, problem solved the traditional way by subclassing QDial and overriding MousePressEvent and MouseReleaseEvent:
#include <QDial>
#include <QMouseEvent>
class QSuperDial : public QDial {
public:
QSuperDial (QWidget *parent = nullptr) : QDial(parent) {
}
virtual void mousePressEvent (QMouseEvent *event) override {
emit sliderPressed();
}
virtual void mouseMoveEvent (QMouseEvent * event) override {
}
virtual void mouseReleaseEvent (QMouseEvent *event) override {
}
};
Promoting QDial to QSuperDial results in a QDial object that 'behaves' like a button when pressed, emitting sliderPressed signal, while still being responsive to MouseWheelEvent (like a normal QDial).
I think this is the simplest and the most 'Qt-like' solution, but please do correct me if I'm mistaken.
You can use QObject::installEventFilter to have the parent object filter the events before they reach the tool button. Then, override the parent's QObject::eventFilter to handle/ignore the event.
I create an example below:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QToolButton>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
bool eventFilter(QObject *watched, QEvent *event) override;
private:
QToolButton tool_button_ignored_;
QToolButton tool_button_handled_;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include <QDebug>
#include <QEvent>
#include <QHBoxLayout>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
tool_button_ignored_.setObjectName("tool_button_ignored_");
tool_button_ignored_.setText("Ignored button");
tool_button_ignored_.installEventFilter(this);
tool_button_handled_.setObjectName("tool_button_handled_");
tool_button_handled_.setText("Handled button");
tool_button_handled_.installEventFilter(this);
QWidget *central_widget = new QWidget{this};
QHBoxLayout *layout = new QHBoxLayout{central_widget};
layout->addWidget(&tool_button_ignored_);
layout->addWidget(&tool_button_handled_);
this->setCentralWidget(central_widget);
}
MainWindow::~MainWindow()
{
}
bool MainWindow::eventFilter(QObject *watched, QEvent *event)
{
if (watched != &tool_button_ignored_ || event->type() != QEvent::Wheel)
{
qDebug() << event->type() << watched->objectName() << "handled";
return QMainWindow::eventFilter(watched, event);
}
else
{
qDebug() << event->type() << watched->objectName() << "ignored";
return true; // stop being handled further
}
}

How to consume QEvent::WindowBlocked so that specific window is always active?

I created myself simple window for debugging purposes. It's completely separate QMainWindow, so that I can put it on the other monitor when testing the app. But when there's a dialog in my application, I cannot access the window. Right now, this is exactly the time I WANT to access it.
I tried to override QWidget::event like so:
DebugWindow.h
#include <QtWidgets/QMainWindow>
QT_BEGIN_NAMESPACE
class QEvent;
QT_END_NAMESPACE
class DebugWindow : public QMainWindow
{
Q_OBJECT
public:
DebugWindow(QWidget *parent = 0);
virtual ~DebugWindow();
protected:
virtual bool event(QEvent*);
};
DebugWindow.cpp
bool TechadminScript::event(QEvent* e)
{
if (e->type() == QEvent::WindowBlocked) {
// accept the event to stop propagation
e->accept();
return true;
}
return false;
}
I have set up breakpoint in the overriden function and it was hit - that means I did something right. But the window is still blocked as it was before.
So I am probably missing something, can somebody help me finish this?
It worked for me setting the window modality as #king_nak suggested. Mind you have to hide() beforehand and show() according to the documentation: https://doc.qt.io/qt-5/qwidget.html#windowModality-prop
bool Object::eventFilter(QObject *, QEvent * const event)
{
if (event->type() != QEvent::WindowBlocked)
return false;
for (auto&& widget : qApp->topLevelWidgets())
{
if (!widget->isModal())
continue;
widget->hide();
widget->setWindowModality(Qt::NonModal);
widget->show();
}
return true;
}

Learn about occurred events from other class

I have two classes:
typedef std::shared_ptr<AdaptedWidget> window_ptr;
class WindowManager
{
public:
...
private:
std::stack<window_ptr> m_windowsStack;
}
and
class AdaptedWidget: public QWidget
{
Q_OBJECT
public:
AdaptedWidget(AdaptedWidget *parent = 0);
bool event(QEvent *event);
};
bool AdaptedWidget::event(QEvent *event)
{
if (event->type() == QEvent::NonClientAreaMouseButtonPress ||
event->type() == QEvent::MouseButtonPress)
{
qDebug() << "mainwindwo press";
}
return QWidget::event(event);
}
I need to get information about events that happen in AdaptedWidget objects from my WindowManager object, How can I do that?
Event filters are the Qt way to accomplish your task.
Make your WindowManager class a subclass of QObject and provide an implementation for its eventFilter() method.
After that, every time you create an AdaptedWidget use installEventFilter() to install your WindowManager instance as an event filter on it.
class WindowManager : public QObject
{
public:
...
bool eventFilter(QObject* obj, QEvent* ev);
private:
std::stack<window_ptr> m_windowsStack;
}
and
bool WindowManager::eventFilter(QObject* obj, QEvent* ev){
AdaptedWidget* widget= qobject_cast<AdaptedWidget*>(obj);
if(ev->type == /*type of event you are interested in*/){
//you can compare widget against the stack of widgets you have
//if you want to stop widget from receiving the event you can return true
}
return false;
}
and when creating each AdaptedWidget instance, install the WindowManager as an event filter:
AdaptedWidget* widget= new AdaptedWidget;
widget->installEventFilter(/*your WindowManager instance*/);
The AdaptedWidget class should have a signal that indicates a mouse press, e.g.
class AdaptedWidget : ... {
Q_OBJECT
...
public:
Q_SIGNAL void mousePress(const QPoint &);
};
bool AdaptedWidget::event(QEvent *event)
{
if (event->type() == QEvent::NonClientAreaMouseButtonPress ||
event->type() == QEvent::MouseButtonPress)
{
auto ev = static_cast<QMouseEvent*>(event);
emit mousePress(ev->pos());
qDebug() << "mainwindow press";
}
return QWidget::event(event);
}
Another approach is to use event filters, but that unnecessarily tightly couples the two classes.

Add a callback for key pressed event in QDialog without extending the class

I have a MainWindow with a QLabel for visualize some QPixmap. I have subclassed the QLabel class in, let's say, MyVisualizer, for handling the QPixmap generation/modification/whatever.
Now in the MainWindow, with a key pressed I want the QLabel to fullscreen, and due to the showFullScreen method works only on windows, I have created a QDialog, moved the myvisualizer instance inside of it, and called QDialog::showFullScreen.Then, I'd like to have another key-pressed listener in the QDialog for some other actions.
Is it possible to add a keyPressEvent(QKeyEvent *e) callback to QDialog without subclassing it?
Yes, you can install an eventFilter.
void QObject::installEventFilter(QObject * filterObj)
class KeyPressEater : public QDialog
{
Q_OBJECT
...
KeyPressEater(Qobject paarent);
protected:
bool eventFilter(QObject *obj, QEvent *event);
};
KeyPressEater::KeyPressEater(Qobject* parent) : QDialog(parent)
{
installEventFilter(this);
}
bool KeyPressEater::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
qDebug("Ate key press %d", keyEvent->key());
return true;
} else {
// standard event processing
return QObject::eventFilter(obj, event);
}
}

How to capture the QDockWidget close button click event

I have a QStackedWidget within a QDockWidget - depending on which page is shown I wish to show/hide the close button on the QDockWidget. I can do this by using QDockWidget::setFeatures().
However the issue I'm facing is how to capture the signal of the close button so that I can change the dock features / set the stacked widget page index.
I have attempted to use an event filter:
class EventFilter : public QObject
{
Q_OBJECT
public:
EventFilter( QObject* aParent );
protected:
bool eventFilter(QObject *obj, QEvent *event);
};
EventFilter::EventFilter( QObject* aParent )
: QObject( aParent )
{
}
bool EventFilter::eventFilter( QObject *obj, QEvent *event )
{
if ( event->type() == QEvent::Close )
{
return true;
}
return QObject::eventFilter( obj, event );
}
And installed it as so:
EventFilter* filter = new EventFilter( this );
u->dockWidget_6->installEventFilter( filter );
In the constructor of my QMainWindow - the eventFilter() method did not get called.
So next I attempted to use the QDockWidget::visibilityChanged changed signal since it sounded like this might be what I wanted:
connect( u->dockWidget_6, SIGNAL(visibilityChanged(bool)), SLOT(dockWindowClosed(bool)) );
This slot did get called - but not when the close button was clicked.
Finally I attempted to use a promoted QDockWidget to capture the QWidget::closeEvent().
class DockWidgetWithCloseSignal : public QDockWidget
{
Q_OBJECT
public:
explicit DockWidgetWithCloseSignal(const QString &title, QWidget *parent = 0, Qt::WindowFlags flags = 0)
: QDockWidget( title, parent, flags )
{
}
explicit DockWidgetWithCloseSignal(QWidget *parent = 0, Qt::WindowFlags flags = 0)
: QDockWidget( parent, flags )
{
}
protected:
void closeEvent(QCloseEvent *event)
{
event->ignore();
}
};
I saw the constructor was called which means this new widget was indeed being used, but again no joy since the closeEvent() was never called when the close button was clicked.
Turns out that everything apart from the visibilityChanged signal works!
I added a signal to the overridden closeEvent() method which I could then connect to any slot I wanted.
The actual issue was that within the stacked widget I had another QDockWidget on another page, hence I was adding all of these things to the wrong QDockWidget! (And of course promoted the wrong QDockWidget too doh!).
Hopefully this question can serve as a reference to anyone else that needs to figure out how to do this - rather than why it isn't working.
Create a new CloseDockWidget based on DockWidget.
Override the closeEvent() method, but emit an additional closed() signal from there.
widgets/qclosedockwidget.cpp:
#include "qclosedockwidget.h"
namespace Widgets
{
QCloseDockWidget::QCloseDockWidget(const QString &title, QWidget *parent)
: QDockWidget(title, parent)
{
// constructor
}
void QCloseDockWidget::closeEvent(QCloseEvent *event)
{
emit closed(); // <------ signal
QDockWidget::closeEvent(event);
}
} // namespace Widgets
widgets/qclosedockwidget.h:
#ifndef QCLOSEDOCKWIDGET_H
#define QCLOSEDOCKWIDGET_H
#include <QDockWidget>
namespace Widgets
{
class QCloseDockWidget : public QDockWidget
{
Q_OBJECT
public:
QCloseDockWidget(const QString &title = "", QWidget *parent = nullptr);
protected:
void closeEvent(QCloseEvent *event);
signals:
void closed();
};
} // namespace Widgets
#endif // QCLOSEDOCKWIDGET_H
Now you are able to instantiate and connect to the new signal:
auto *dockWidget = new Widgets::QCloseDockWidget("MyDockWidget", this);
connect(dockWidget, &Widgets::QCloseDockWidget::closed, this, &MainWindow::dockWidgetCloseClicked);