-
Notifications
You must be signed in to change notification settings - Fork 366
/
Copy pathMyMouseArea.h
62 lines (57 loc) · 1.79 KB
/
MyMouseArea.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#pragma once
#include <QCoreApplication>
#include <QHoverEvent>
// quick-private
#include <QtQuick/private/qquickmousearea_p.h>
/**
* @brief 继承QQuickMouseArea并转发需要的事件
* @author 龚建波
* @date 2024-04-24
*/
class MyMouseArea : public QQuickMouseArea
{
Q_OBJECT
Q_PROPERTY(QQuickMouseArea *forwardTo READ getForwardTo WRITE setForwardTo NOTIFY forwardToChanged)
public:
using QQuickMouseArea::QQuickMouseArea;
QQuickMouseArea *getForwardTo() const {
return forwardTo;
}
void setForwardTo(QQuickMouseArea *forward) {
if (forwardTo != forward) {
forwardTo = forward;
emit forwardToChanged();
}
}
protected:
void hoverEnterEvent(QHoverEvent *event) override {
QQuickMouseArea::hoverEnterEvent(event);
doForward(event);
}
void hoverMoveEvent(QHoverEvent *event) override {
QQuickMouseArea::hoverMoveEvent(event);
doForward(event);
}
void hoverLeaveEvent(QHoverEvent *event) override {
QQuickMouseArea::hoverLeaveEvent(event);
doForward(event);
}
void doForward(QHoverEvent *event) {
if (!forwardTo) {
return;
}
// TODO 这里没有处理交叠部分,而是直接转发了
QHoverEvent hover{event->type(),
mapToItem(forwardTo, event->posF()),
mapToItem(forwardTo, event->oldPosF()),
event->modifiers()};
// 使用send多个MouseArea之间移动时保持一定的处理顺序
QCoreApplication::sendEvent(forwardTo, &hover);
// QCoreApplication::sendEvent(forwardTo, event);
}
signals:
void forwardToChanged();
private:
// 事件转发到对应的MouseArea
QQuickMouseArea *forwardTo{nullptr};
};