-
Notifications
You must be signed in to change notification settings - Fork 0
/
myitem.cpp
177 lines (164 loc) · 4.86 KB
/
myitem.cpp
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "myitem.h"
#include <QPainter>
#include <QCursor>
#include <QKeyEvent>
#include <QGraphicsSceneEvent>
#include <QGraphicsSceneContextMenuEvent>
#include <QMenu>
#include <QGraphicsEffect>
MyItem::MyItem()
{
setFlag(QGraphicsItem::ItemIsFocusable);
setFlag(QGraphicsItem::ItemIsMovable);
setAcceptHoverEvents(true);
}
QRectF MyItem::boundingRect() const
{
qreal adjust=0.5;
return QRectF(-10-adjust,-10-adjust,20+adjust,20+adjust);
}
void MyItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)//执行实际的绘图操作
{
if(hasFocus()||!collidingItems().isEmpty())//如果获得焦点或者图形项列表不为空
{
painter->setPen(QPen(QColor(255,255,255,200)));
}
else
{
painter->setPen(QPen(QColor(100,100,100,100)));
}
painter->setBrush(brushColor);//使用变量作为画刷的颜色,可以动态指定图形项的填充色
painter->drawRect(-10,-10,20,20);
}
//鼠标按下事件处理函数,设置边点击的图形项获得焦点,并改变光标外观
void MyItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
setFocus();
setCursor(Qt::ClosedHandCursor);
}
//键盘按下事件处理函数,判断是否是"-"键,如果是,则向下移动图形项
void MyItem::keyPressEvent(QKeyEvent *event)
{
if(event->key()==Qt::Key_S)
{
moveBy(0,10);
}
else if(event->key()==Qt::Key_W)
{
moveBy(0,-10);
}
else if(event->key()==Qt::Key_A)
{
moveBy(-10,0);
}
else if(event->key()==Qt::Key_D)
{
moveBy(10,0);
}
/*
*QGraphicsBlurEffect类提供了一个模糊效果
*QGraphicsColorizeEffect类提供了一个染色效果
*QGraphicsDropShadowEffect类提供了一个阴影效果
*QGraphicsOpacityEffect类提供了一个透明效果
*/
switch (event->key())
{
case Qt::Key_1:
{
QGraphicsBlurEffect *blurEffect=new QGraphicsBlurEffect;
blurEffect->setBlurHints(QGraphicsBlurEffect::QualityHint);//指定模糊怎样来执行
blurEffect->setBlurRadius(8);//修改细节等级,默认的模糊半径是5像素
setGraphicsEffect(blurEffect);
break;
}
case Qt::Key_2:
{
QGraphicsColorizeEffect *colorizeEffect=new QGraphicsColorizeEffect;
colorizeEffect->setColor(Qt::white);//修改颜色,默认是浅紫色
colorizeEffect->setStrength(0.6);//修改效果强度
setGraphicsEffect(colorizeEffect);
break;
}
case Qt::Key_3:
{
QGraphicsDropShadowEffect *dropShadowEffect=new QGraphicsDropShadowEffect;
dropShadowEffect->setColor(QColor(63,63,63,100));//修改阴影颜色,默认是透明的黑灰色
dropShadowEffect->setBlurRadius(2);//改变模糊半径
dropShadowEffect->setOffset(10);//改变阴影偏移值
setGraphicsEffect(dropShadowEffect);
break;
}
case Qt::Key_4:
{
QGraphicsOpacityEffect *opacityEffect=new QGraphicsOpacityEffect;
opacityEffect->setOpacity(0.4);//修改透明度
setGraphicsEffect(opacityEffect);
break;
}
case Qt::Key_5:
{
graphicsEffect()->setEnabled(false);
break;
}
}
}
//悬停事件处理函数,设置光标外观
void MyItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
setCursor(Qt::OpenHandCursor);
setToolTip("This is a item");
}
//右键菜单事件处理函数,为图形项添加一个右键菜单
void MyItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
{
QMenu menu;
QAction *moveAction=menu.addAction("move back");
QAction *seclectedAction=menu.exec(event->screenPos());
if(seclectedAction==moveAction)
{
setPos(0,0);
}
}
//动画
void MyItem::advance(int phase)
{
/*
*调用场景的advance()函数就会自动调用场景中所有图形项的advance()函数,
*而且图形项的advance()函数会被分为两个阶段调用两次,第一次phase为0,
*告知所有图形项场景将要改变;第二次phase为1,在这时才进行具体的操作
*
*/
//在第一个阶段不进行处理
if(!phase)
{
return ;
}
//图形项向不同方向随机移动
int value=qrand()%100;
if(value<25)
{
//rotate(45);
moveBy(qrand()%10,qrand()%10);
}
else if(value<50)
{
//rotate(-45);
moveBy(-qrand()%10,-qrand()%10);
}
else if(value<75)
{
//rotate(30);
moveBy(-qrand()%10,qrand()%10);
}
else
{
//rotate(-30);
moveBy(qrand()%10,-qrand()%10);
}
}
QPainterPath MyItem::shape()
{
QPainterPath path;
path.addRect(-10,-10,20,20);
return path;
}