-
Notifications
You must be signed in to change notification settings - Fork 0
/
customgraphicsview.h
49 lines (42 loc) · 1.3 KB
/
customgraphicsview.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
#ifndef CUSTOMGRAPHICSVIEW_H
#define CUSTOMGRAPHICSVIEW_H
#include <QGraphicsView>
#include <functional>
#include <QMenu>
#include <QGraphicsSceneMouseEvent>
class CustomGraphicsView: public QGraphicsView {
public:
CustomGraphicsView(QWidget*parent=0):QGraphicsView(parent) {
}
};
class CustomGraphicsScene:public QGraphicsScene {
std::function<void(QMenu*menu,QPointF screenPos)>contextMenuGenerator;
protected:
void mousePressEvent(QGraphicsSceneMouseEvent* event)override{
if (event->button() == Qt::RightButton) {
event->accept();
return;
}
QGraphicsScene::mousePressEvent(event);
/*
QGraphicsItem* item = itemAt(event->scenePos(), QTransform());
if (item != nullptr && !item->isSelected())
{
item->setSelected(1);
}
*/
}
void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) override {
if(!contextMenuGenerator) {
return;
}
QMenu menu;
contextMenuGenerator(&menu, event->scenePos());
menu.exec(event->screenPos());
}
public:
void setContextMenuGenerator(std::function<void(QMenu*menu,QPointF screenPos)>contextMenuGenerator) {
this->contextMenuGenerator=contextMenuGenerator;
}
};
#endif // CUSTOMGRAPHICSVIEW_H