QMouseEvent - is it press or release or drag?
-
@Pl45m4 - I'm creating several apps that display a QPixmap within a QLabel; users interact with that QPixmap with the mouse. I've designed ClickableLabel which can be used by these apps; this widget emits a signal with the QMouseEvent as payload, then various apps can processes the event in a the app-specific manner (this 'business logic' isn't necessarily contained in a Qt widget). Does this seem reasonable? Thanks!
@Tom-asso said in QMouseEvent - is it press or release or drag?:
I've designed ClickableLabel which can be used by these apps; this widget emits a signal with the QMouseEvent as payload, then various apps can processes the event in a the app-specific manner (this 'business logic' isn't necessarily contained in a Qt widget).
The
ClickableLabel
has the event handlermousePressEvent
. So why do you want to transfer theQMouseEvent
within the signal?
You can/should get the information you need (like mouse position) in the handler function and then emit a signal likevoid imageClicked(QPoint pos)
, where you send theevent->pos()
(orevent->position()
if Qt6+) from the handler to the receiving widget.As I've mentioned before, it's unusal to send a
QEvent
via signal-slots.
(If I got it right what you are doing there)Edit:
The example is already prepared for this:void ClickableLabel::mousePressEvent(QMouseEvent* event) { // add this QPointF pos = event->position(); // some further processing if needed // ... emit clicked(pos); // change signal to "void clicked(QPointF)" }
-
Is it possible to tell if a QMouseEvent itself was generated by press, release, or drag? QMouseEvent constructors include a QEvent::Type argument, but how can I retrieve that type info after the QMouseEvent is constructed? Thanks!
If you receive the event, you can filter/check it.
For example with an eventFilter, which you install on some widget.
QWidget
has separate event handlers likeQWidget::mousePressEvent(QMouseEvent*ev)
(same for release and move) or you can compare the transportedQEvent ev
anytime toQEvent::MouseButtonDblClick
Mouse press again (QMouseEvent).QEvent::MouseButtonPress
Mouse press (QMouseEvent).QEvent::MouseButtonRelease
Mouse release (QMouseEvent).QEvent::MouseMove
Mouse move (QMouseEvent).
(https://doc.qt.io/qt-6/qevent.html#Type-enum)
Whether you use an
eventFilter
or better re-implement the event handlers depends on your use case. -
If you receive the event, you can filter/check it.
For example with an eventFilter, which you install on some widget.
QWidget
has separate event handlers likeQWidget::mousePressEvent(QMouseEvent*ev)
(same for release and move) or you can compare the transportedQEvent ev
anytime toQEvent::MouseButtonDblClick
Mouse press again (QMouseEvent).QEvent::MouseButtonPress
Mouse press (QMouseEvent).QEvent::MouseButtonRelease
Mouse release (QMouseEvent).QEvent::MouseMove
Mouse move (QMouseEvent).
(https://doc.qt.io/qt-6/qevent.html#Type-enum)
Whether you use an
eventFilter
or better re-implement the event handlers depends on your use case. -
@Pl45m4 - Thanks!
I've designed a signal that emits just the QMouseEvent. EDIT: So the slot can just call event->type() function and filter on that.@Tom-asso said in QMouseEvent - is it press or release or drag?:
I've designed a signal that emits just the QMouseEvent.
Then design again please :)
No, really. The Qt Event system and the Signal/Slot mechanism are two different things and I can't imagine any case where you need to send an event by emitting a signal. Makes no sense.(This would also make the event kinda useless, since it's sent to the receiving widget directly and is not processed by Qt's event handlers and passed up/down the object tree).
If you have a custom event type (rarely an input event like
QMouseEvent
), you can create an event yourself and post/send it withQApplication::sendEvent()
orQApplication::postEvent()
- https://doc.qt.io/qt-6/qcoreapplication.html#sendEvent
- https://doc.qt.io/qt-6/qcoreapplication.html#postEvent
The
sendEvent
documentation even includes an example where a mouse click is simulated by constructing a mouse press event and sent to an certain widget.
But usually you don't need to do that
(maybe in test cases, but theQTest
framework has its own functions to simulate mouse clicks or button presses)QMouseEvent event(QEvent::MouseButtonPress, pos, 0, 0, 0); QApplication::sendEvent(mainWindow, &event);
Edit:
EDIT: So the slot can just call event->type() function and filter on that.
If this is your use case, you can simply re-implement the handlers where your slot is. Then you don't even need to filter, since there is:
QWidget::mouseReleaseEvent(QMouseEvent *event)
QWidget::mousePressEvent(QMouseEvent *event)
and
QWidget::mouseMoveEvent(QMouseEvent *event)
while you still have access to the
event
inside the handler function to get the position where the event occured, for example. -
Is it possible to tell if a QMouseEvent itself was generated by press, release, or drag? QMouseEvent constructors include a QEvent::Type argument, but how can I retrieve that type info after the QMouseEvent is constructed? Thanks!
@Tom-asso said in QMouseEvent - is it press or release or drag?:
but how can I retrieve that type info after the QMouseEvent is constructed?
By calling https://doc.qt.io/qt-6/qevent.html#type
-
@Tom-asso said in QMouseEvent - is it press or release or drag?:
I've designed a signal that emits just the QMouseEvent.
Then design again please :)
No, really. The Qt Event system and the Signal/Slot mechanism are two different things and I can't imagine any case where you need to send an event by emitting a signal. Makes no sense.(This would also make the event kinda useless, since it's sent to the receiving widget directly and is not processed by Qt's event handlers and passed up/down the object tree).
If you have a custom event type (rarely an input event like
QMouseEvent
), you can create an event yourself and post/send it withQApplication::sendEvent()
orQApplication::postEvent()
- https://doc.qt.io/qt-6/qcoreapplication.html#sendEvent
- https://doc.qt.io/qt-6/qcoreapplication.html#postEvent
The
sendEvent
documentation even includes an example where a mouse click is simulated by constructing a mouse press event and sent to an certain widget.
But usually you don't need to do that
(maybe in test cases, but theQTest
framework has its own functions to simulate mouse clicks or button presses)QMouseEvent event(QEvent::MouseButtonPress, pos, 0, 0, 0); QApplication::sendEvent(mainWindow, &event);
Edit:
EDIT: So the slot can just call event->type() function and filter on that.
If this is your use case, you can simply re-implement the handlers where your slot is. Then you don't even need to filter, since there is:
QWidget::mouseReleaseEvent(QMouseEvent *event)
QWidget::mousePressEvent(QMouseEvent *event)
and
QWidget::mouseMoveEvent(QMouseEvent *event)
while you still have access to the
event
inside the handler function to get the position where the event occured, for example.@Pl45m4 - I'm creating several apps that display a QPixmap within a QLabel; users interact with that QPixmap with the mouse. I've designed ClickableLabel which can be used by these apps; this widget emits a signal with the QMouseEvent as payload, then various apps can processes the event in a the app-specific manner (this 'business logic' isn't necessarily contained in a Qt widget). Does this seem reasonable? Thanks!
-
@Tom-asso said in QMouseEvent - is it press or release or drag?:
but how can I retrieve that type info after the QMouseEvent is constructed?
By calling https://doc.qt.io/qt-6/qevent.html#type
-
@Pl45m4 - I'm creating several apps that display a QPixmap within a QLabel; users interact with that QPixmap with the mouse. I've designed ClickableLabel which can be used by these apps; this widget emits a signal with the QMouseEvent as payload, then various apps can processes the event in a the app-specific manner (this 'business logic' isn't necessarily contained in a Qt widget). Does this seem reasonable? Thanks!
@Tom-asso said in QMouseEvent - is it press or release or drag?:
I've designed ClickableLabel which can be used by these apps; this widget emits a signal with the QMouseEvent as payload, then various apps can processes the event in a the app-specific manner (this 'business logic' isn't necessarily contained in a Qt widget).
The
ClickableLabel
has the event handlermousePressEvent
. So why do you want to transfer theQMouseEvent
within the signal?
You can/should get the information you need (like mouse position) in the handler function and then emit a signal likevoid imageClicked(QPoint pos)
, where you send theevent->pos()
(orevent->position()
if Qt6+) from the handler to the receiving widget.As I've mentioned before, it's unusal to send a
QEvent
via signal-slots.
(If I got it right what you are doing there)Edit:
The example is already prepared for this:void ClickableLabel::mousePressEvent(QMouseEvent* event) { // add this QPointF pos = event->position(); // some further processing if needed // ... emit clicked(pos); // change signal to "void clicked(QPointF)" }
-
T Tom asso has marked this topic as solved on