Qt5 图形视图框架QGraphicsView

1、图形视图框架包含三大类:场景类(QGraphicsScene),视图类(QGraphicsView),图元类(QGraphicsItem); 
2、对于有很多项的场景,使用轻量级的QGraphicsItem类作为数目众多的自定义项的基础最好,仅对数目较少的项使用QGraphicsObject 
3、当一个项发生了变换(transformation),该项所有的子对象自动应用 该变换,递归应用到最深层次的子孙对象, 
变换有(拖动,移动。。。。。),通过调用QGraphicsItem::setFlag(QGraphicsItem::ItemIngnoreTransformation)可以使子项忽略父项的变换,通常使用的标志还有可移动,选中,获得焦点等 
4、通过把项设置为QGraphicsItemGroup的子项,可以将他们分组,创建项的集合。 
5、图形视图类使用三种坐标系统,但是实际应用中只是关心其中的两种。视图使用物理坐标系统,场景使用在构造函数中传入的QRectF定义的逻辑坐标系统。Qt自动进行场景坐标到视图坐标的映射,放置项时使用的是场景的坐标。项的中心是在(0,0)点的逻辑坐标系统,每个项的(0,0)点实际在该项场景中所在位置的中心(文本项除外,原点在项的左上角)。 
6、图形项:QGraphicsItem因为有两个纯虚函数而不能实例化对象,这两个纯虚函数是boundingRect()和paint() 
paint()函数必须重新实现来绘制项,boundingRect()函数在图形视图框架中为项提供一个边界矩形,用于冲突监测和保证项仅在QGraphicsView的视口(viewport)中可见时才重绘。如果要创建非矩形形状的自定义图形项,最好同时实现shape()方法,这个方法返回一个精确描述项的轮廓的QpainterPath,对于准确检测冲突和鼠标点击非常有用。 
7、如果要自定义一个形状,最简单的是使用标准的QGraphicsItem的子类之一,比如QGraphicsPathItem和QGraphicsPolygonItem,如果有定制行为,可以重新实现保护事件的处理函数,如keyPressEvent()。如果只是想要自己绘制,可以从QGraphicsItem派生,重新实现boundingRect()和paint()和shape()方法。

俄罗斯方块的实现

俄罗斯方块的逻辑

小方块的类型

效果图

 

代码的实现

onePiece.h

  1. #ifndef ONEPIECE_H
  2. #define ONEPIECE_H
  3. /************
  4. *一块方块的绘制
  5. *
  6. *
  7. ***************/
  8. #include <QGraphicsObject>
  9. #include <QColor>
  10. class onePiece : public QGraphicsObject
  11. {
  12. public:
  13. onePiece(const QColor &brushColor = Qt::red);
  14. //为项提供一个外围的边界矩形
  15. virtual QRectF boundingRect() const;
  16. //QGraphicsView调用,在本地坐标系统中绘制项
  17. virtual void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0);
  18. //重新定义项的形状,默认调用boundingRect获取项的矩形形状轮廓
  19. virtual QPainterPath shape() const;
  20. private:
  21. QColor m_brushColor;
  22. };
  23. #endif // ONEPIECE_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
  • 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

onepiece.c

  1. #include "onepiece.h"
  2. #include <QPainter>
  3. #include "enumHeader.h"
  4. /****
  5. * 为了避免同一个方块组内小方块发生碰撞,
  6. * 小方块的大小实际为19.5*19.5,但是小方块加上笔刷的宽度后为20*20
  7. * 这样看起来是四个小方块连在一起的
  8. * **/
  9. onePiece::onePiece(const QColor &brushColor)
  10. :m_brushColor(brushColor)
  11. {
  12. }
  13. QRectF onePiece::boundingRect() const
  14. {
  15. qreal penWidth = PEN_WIDTH;
  16. return QRectF(-(PIECE_DIAMETER - penWidth)/2, -(PIECE_DIAMETER - penWidth)/2,
  17. PIECE_DIAMETER - penWidth, PIECE_DIAMETER - penWidth);
  18. }
  19. void onePiece::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
  20. {
  21. //背景贴图
  22. painter->drawPixmap(-PIECE_DIAMETER/2,-PIECE_DIAMETER/2,PIECE_DIAMETER,PIECE_DIAMETER,QPixmap(":/piece/Image/piece/box.png"));
  23. painter->setBrush(m_brushColor);
  24. //将方块的边界的颜色进行透明化
  25. QColor penColor = m_brushColor;
  26. // 将颜色的透明度减小
  27. penColor.setAlpha(200);
  28. painter->setPen(penColor);
  29. //使用当前的笔刷和笔画矩形框
  30. painter->drawRect(-PIECE_DIAMETER/2, -PIECE_DIAMETER/2, PIECE_DIAMETER, PIECE_DIAMETER);
  31. }
  32. QPainterPath onePiece::shape() const
  33. {
  34. QPainterPath path;
  35. //去除笔刷的宽度,这样同一个方块组的方块就不会被检测出碰撞的情况
  36. path.addRect(-(PIECE_DIAMETER-PEN_WIDTH)/2,-(PIECE_DIAMETER-PEN_WIDTH)/2,PIECE_DIAMETER-PEN_WIDTH,PIECE_DIAMETER-PEN_WIDTH);
  37. return path;
  38. }
  • 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
  • 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

pieceBox.h

  1. #ifndef PIECEBOX_H
  2. #define PIECEBOX_H
  3. /********
  4. * 方块组
  5. * 4*4
  6. *
  7. *
  8. * ******/
  9. #include <QGraphicsItemGroup>
  10. #include <QKeyEvent>
  11. #include <QTimer>
  12. #include "enumHeader.h"
  13. #include <QTransform>
  14. #include "onepiece.h"
  15. #include <QGraphicsItem>
  16. class pieceBox :public QObject,public QGraphicsItemGroup
  17. {
  18. Q_OBJECT
  19. public:
  20. pieceBox();
  21. //颜色表
  22. static QColor colorTable[7];
  23. //绘制方块组的边框矩形
  24. virtual QRectF boundingRect() const;
  25. //是否发生碰撞
  26. bool isCollding() const;
  27. //获取当前的方块类型
  28. BoxType getCurrentBoxType() const;
  29. //创建方块组
  30. void createBox(const QPointF &point = QPointF(0,0),
  31. BoxType currentBoxType = RandomShape);
  32. //消除方块组
  33. void clearBoxGroup(const bool &isClear = false);
  34. protected:
  35. virtual void keyPressEvent(QKeyEvent * event);
  36. signals:
  37. void signal_needNewBox();
  38. void signal_gameOver();
  39. public slots:
  40. void slot_moveOneStep();
  41. void slot_startTimer(int timeSec);
  42. void slot_stopTimer();
  43. private:
  44. QTimer *m_Timer;
  45. BoxType m_currentBoxType;
  46. QTransform m_oldTransform;
  47. QList<onePiece *> m_pieceBoxList; //存放新方块组的四个方块
  48. };
  49. #endif // PIECEBOX_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
  • 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

pieceBox.c

  1. #include "piecebox.h"
  2. #include "enumHeader.h"
  3. #include <QList>
  4. #include <QDebug>
  5. QColor pieceBox::colorTable[7] = {QColor(200,0,0,100),QColor(255,200,0,100),
  6. QColor(0,0,200,100),QColor(0,200,0,100),
  7. QColor(0,200,255,100),QColor(200,0,255,100),
  8. QColor(150,100,100,100)};
  9. pieceBox::pieceBox()
  10. {
  11. m_pieceBoxList.clear();
  12. //使得方块组支持键盘操作
  13. setFlags(QGraphicsItem::ItemIsFocusable);
  14. //保存矩阵的原状态,使得可以还原矩阵
  15. m_oldTransform = transform();
  16. m_Timer = new QTimer(this);
  17. connect(m_Timer,SIGNAL(timeout()),this,SLOT(slot_moveOneStep()));
  18. m_currentBoxType = RandomShape;
  19. }
  20. //绘制方块组的边框矩形
  21. QRectF pieceBox::boundingRect() const
  22. {
  23. qreal penWidth = PEN_WIDTH;
  24. return QRectF(-(PIECE_DIAMETER*2 - penWidth) / 2, -(PIECE_DIAMETER*2 - penWidth) / 2,
  25. PIECE_DIAMETER*4-penWidth, PIECE_DIAMETER*4-penWidth);
  26. }
  27. bool pieceBox::isCollding() const
  28. {
  29. //依次检测方块组中的每个方块的碰撞
  30. foreach (onePiece *piece, m_pieceBoxList) {
  31. // qDebug() << "colliding number:" << piece->collidingItems().count();
  32. //获取与该方块碰撞的方块的个数
  33. if(piece->collidingItems().count() > 1)
  34. {
  35. return true;
  36. }
  37. }
  38. return false;
  39. }
  40. BoxType pieceBox::getCurrentBoxType() const
  41. {
  42. return m_currentBoxType;
  43. }
  44. void pieceBox::createBox(const QPointF &point, BoxType currentBoxType)
  45. {
  46. int boxType = currentBoxType;
  47. if(boxType == RandomShape)
  48. {
  49. boxType = qrand() % 7;
  50. }
  51. //设置该方块组的颜色
  52. QColor boxColor = colorTable[boxType];
  53. //准备四个小方块
  54. //恢复方块组的变换矩阵,因为之前的方块组的变换会导致变换矩阵的位置发生变换,
  55. //防止新的方块组因为旧的方块组变换异常
  56. setTransform(m_oldTransform);
  57. setRotation(0);
  58. resetTransform();
  59. m_pieceBoxList.clear();
  60. for(int i = 0;i < 4;i++)
  61. {
  62. onePiece *piece = new onePiece(boxColor);
  63. m_pieceBoxList.append(piece);
  64. //将小方块添加到方块组中
  65. addToGroup(piece);
  66. }
  67. //设置方块组的形状
  68. switch (boxType) {
  69. case IShape:
  70. m_currentBoxType = IShape;
  71. m_pieceBoxList.at(0)->setPos(-30,-10);
  72. m_pieceBoxList.at(1)->setPos(-10,-10);
  73. m_pieceBoxList.at(2)->setPos(10,-10);
  74. m_pieceBoxList.at(3)->setPos(30,-10);
  75. break;
  76. case JShape:
  77. m_currentBoxType = JShape;
  78. m_pieceBoxList.at(0)->setPos(10,-10);
  79. m_pieceBoxList.at(1)->setPos(10,10);
  80. m_pieceBoxList.at(2)->setPos(10,30);
  81. m_pieceBoxList.at(3)->setPos(-10,30);
  82. break;
  83. case LShape:
  84. m_currentBoxType = LShape;
  85. m_pieceBoxList.at(0)->setPos(-10,-10);
  86. m_pieceBoxList.at(1)->setPos(-10,10);
  87. m_pieceBoxList.at(2)->setPos(10,30);
  88. m_pieceBoxList.at(3)->setPos(-10,30);
  89. break;
  90. case OShape:
  91. m_currentBoxType = OShape;
  92. m_pieceBoxList.at(0)->setPos(-10,-10);
  93. m_pieceBoxList.at(1)->setPos(10,-10);
  94. m_pieceBoxList.at(2)->setPos(-10,10);
  95. m_pieceBoxList.at(3)->setPos(10,10);
  96. break;
  97. case SShape:
  98. m_currentBoxType = SShape;
  99. m_pieceBoxList.at(0)->setPos(30,-10);
  100. m_pieceBoxList.at(1)->setPos(10,-10);
  101. m_pieceBoxList.at(2)->setPos(10,10);
  102. m_pieceBoxList.at(3)->setPos(-10,10);
  103. break;
  104. case TShape:
  105. m_currentBoxType = TShape;
  106. m_pieceBoxList.at(0)->setPos(-10,-10);
  107. m_pieceBoxList.at(1)->setPos(10,-10);
  108. m_pieceBoxList.at(2)->setPos(30,-10);
  109. m_pieceBoxList.at(3)->setPos(10,10);
  110. break;
  111. case Zshape:
  112. m_currentBoxType = Zshape;
  113. m_pieceBoxList.at(0)->setPos(-10,-10);
  114. m_pieceBoxList.at(1)->setPos(10,-10);
  115. m_pieceBoxList.at(2)->setPos(10,10);
  116. m_pieceBoxList.at(3)->setPos(30,10);
  117. break;
  118. default:
  119. break;
  120. }
  121. //设置方块组的位置
  122. setPos(point);
  123. //检测是否发生碰撞,发生碰撞,游戏结束
  124. if(isCollding())
  125. {
  126. slot_stopTimer();
  127. emit signal_gameOver();
  128. }
  129. }
  130. //删除方块组中的小方块到情景中
  131. void pieceBox::clearBoxGroup(const bool &isClear)
  132. {
  133. QList<QGraphicsItem *> itemList = childItems();
  134. foreach (QGraphicsItem *piece, itemList) {
  135. removeFromGroup(piece);
  136. // qDebug() << "clear box group" << isClear;
  137. if(isClear)
  138. {
  139. //销毁小方块
  140. ((onePiece *)piece)->deleteLater();
  141. }
  142. }
  143. m_pieceBoxList.clear();
  144. }
  145. void pieceBox::keyPressEvent(QKeyEvent *event)
  146. {
  147. switch (event->key()) {
  148. case Qt::Key_Down:
  149. moveBy(0,20);
  150. //检测是否碰撞
  151. if(isCollding())
  152. {
  153. moveBy(0,-20);
  154. clearBoxGroup();
  155. //需要新的方块
  156. emit signal_needNewBox();
  157. }
  158. break;
  159. case Qt::Key_Left:
  160. moveBy(-20,0);
  161. if(isCollding())
  162. {
  163. moveBy(20,0);
  164. }
  165. break;
  166. case Qt::Key_Right:
  167. moveBy(20,0);
  168. if(isCollding())
  169. {
  170. moveBy(-20,0);
  171. }
  172. break;
  173. case Qt::Key_Up:
  174. setRotation(rotation()+90);
  175. if(isCollding())
  176. {
  177. setRotation(rotation()-90);
  178. }
  179. break;
  180. case Qt::Key_Space: //快速下落
  181. moveBy(0,20);
  182. while (!isCollding()) {
  183. moveBy(0,20);
  184. }
  185. moveBy(0,-20);
  186. clearBoxGroup();
  187. //需要新的方块
  188. emit signal_needNewBox();
  189. break;
  190. default:
  191. break;
  192. }
  193. }
  194. void pieceBox::slot_moveOneStep()
  195. {
  196. moveBy(0,20);
  197. // qDebug() << "move one step";
  198. if(isCollding())
  199. {
  200. moveBy(0,-20);
  201. // 将小方块从方块组中移除到场景中
  202. clearBoxGroup();
  203. //需要新的方块
  204. emit signal_needNewBox();
  205. }
  206. }
  207. void pieceBox::slot_startTimer(int timeSec)
  208. {
  209. m_Timer->start(timeSec);
  210. }
  211. void pieceBox::slot_stopTimer()
  212. {
  213. m_Timer->stop();
  214. }
  • 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
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 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
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229

**界面类**panel.h

  1. #ifndef PANEL_H
  2. #define PANEL_H
  3. #include <QWidget>
  4. #include <QPalette>
  5. #include <QPixmap>
  6. #include <QGraphicsScene>
  7. #include <QGraphicsView>
  8. #include "piecebox.h"
  9. #include "onepiece.h"
  10. #include <QGraphicsLineItem>
  11. #include <QMediaPlayer>
  12. namespace Ui {
  13. class Panel;
  14. }
  15. class Panel : public QWidget
  16. {
  17. Q_OBJECT
  18. public:
  19. explicit Panel(QWidget *parent = 0);
  20. ~Panel();
  21. void setbackground(const int &Lv);
  22. void initControlWidget();
  23. void initGraphicsViewWidget();
  24. /**游戏控制项***/
  25. void startGame();
  26. void pauseGame();
  27. void restartGame();
  28. void stopGame();
  29. void updateScore(const int fullRows = 0);
  30. private slots:
  31. void on_pbt_startGame_clicked();
  32. void on_pbt_pauseGame_clicked();
  33. void on_pbt_restart_clicked();
  34. void on_pbt_stopGame_clicked();
  35. void slot_clearFullRows();
  36. void slot_gameOver();
  37. void slot_moveBox();
  38. private:
  39. Ui::Panel *ui;
  40. QPalette m_Palette;
  41. QPixmap m_pixmap;
  42. int m_currentLv;
  43. int m_currentLVSpeed;
  44. bool m_isPauseGame;
  45. bool m_isGameOver;
  46. QGraphicsScene *m_scene;
  47. //四条边界线
  48. QGraphicsLineItem *m_topLine;
  49. QGraphicsLineItem *m_leftLine;
  50. QGraphicsLineItem *m_buttomLine;
  51. QGraphicsLineItem *m_rightLine;
  52. pieceBox *m_currentBox;
  53. pieceBox *m_nextBox;
  54. QList<int> m_rowList;
  55. QMediaPlayer *m_mediaPlayer;
  56. };
  57. #endif // PANEL_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
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 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

panel.c

  1. #include "panel.h"
  2. #include "ui_panel.h"
  3. #include "enumHeader.h"
  4. #include <QDebug>
  5. #include <QGraphicsBlurEffect>
  6. #include <QPropertyAnimation>
  7. #include <QMessageBox>
  8. #include <QMediaContent>
  9. Panel::Panel(QWidget *parent) :
  10. QWidget(parent),
  11. ui(new Ui::Panel)
  12. {
  13. ui->setupUi(this);
  14. m_isPauseGame = false;
  15. m_isGameOver = false;
  16. m_mediaPlayer = new QMediaPlayer;
  17. m_mediaPlayer->setMedia(QUrl::fromLocalFile(QString("clearRow.mp3")));
  18. m_mediaPlayer->setVolume(100);
  19. //设置背景
  20. setbackground(LV0);
  21. initControlWidget();
  22. initGraphicsViewWidget();
  23. }
  24. Panel::~Panel()
  25. {
  26. delete ui;
  27. }
  28. void Panel::setbackground(const int &Lv)
  29. {
  30. setAutoFillBackground(true);
  31. switch (Lv) {
  32. case LV0:
  33. m_currentLv = LV0;
  34. m_currentLVSpeed = LV0_SPEED;
  35. ui->label_gameLevel->setText("俄罗斯方块");
  36. m_pixmap.load(":/background/Image/background/background.png");
  37. break;
  38. case LV1:
  39. m_currentLv = LV1;
  40. m_currentLVSpeed = LV1_SPEED;
  41. ui->label_gameLevel->setText("第一关");
  42. m_pixmap.load(":/background/Image/background/background01.png");
  43. break;
  44. case LV2:
  45. m_currentLv = LV2;
  46. m_currentLVSpeed = LV2_SPEED;
  47. ui->label_gameLevel->setText("第二关");
  48. m_pixmap.load(":/background/Image/background/background02.png");
  49. break;
  50. case LV3:
  51. m_currentLv = LV3;
  52. m_currentLVSpeed = LV3_SPEED;
  53. ui->label_gameLevel->setText("第三关");
  54. m_pixmap.load(":/background/Image/background/background03.png");
  55. break;
  56. case LV4:
  57. m_currentLv = LV4;
  58. m_currentLVSpeed = LV4_SPEED;
  59. ui->label_gameLevel->setText("第四关");
  60. m_pixmap.load(":/background/Image/background/background04.png");
  61. break;
  62. case LV5:
  63. m_currentLv = LV5;
  64. m_currentLVSpeed = LV5_SPEED;
  65. ui->label_gameLevel->setText("第五关");
  66. m_pixmap.load(":/background/Image/background/background05.png");
  67. break;
  68. default:
  69. break;
  70. }
  71. m_Palette.setBrush(QPalette::Window, QBrush(m_pixmap.scaled(size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation)));
  72. setPalette(m_Palette);
  73. }
  74. void Panel::initControlWidget()
  75. {
  76. switch (m_currentLv) {
  77. case LV0:
  78. ui->pbt_startGame->setEnabled(true);
  79. ui->pbt_pauseGame->setEnabled(false);
  80. ui->pbt_restart->setEnabled(false);
  81. ui->pbt_stopGame->setEnabled(false);
  82. break;
  83. case LV1:
  84. case LV2:
  85. case LV3:
  86. case LV4:
  87. case LV5:
  88. ui->pbt_startGame->setEnabled(false);
  89. ui->pbt_pauseGame->setEnabled(true);
  90. ui->pbt_restart->setEnabled(true);
  91. ui->pbt_stopGame->setEnabled(true);
  92. break;
  93. default:
  94. break;
  95. }
  96. }
  97. void Panel::initGraphicsViewWidget()
  98. {
  99. //使用抗锯齿渲染
  100. ui->GraphicsView->setRenderHint(QPainter::Antialiasing);
  101. //设置缓冲背景,加速渲染
  102. ui->GraphicsView->setCacheMode(QGraphicsView::CacheBackground);
  103. m_scene = new QGraphicsScene(this);
  104. m_scene->setSceneRect(30,30,310,410);
  105. ui->GraphicsView->setScene(m_scene);
  106. //方块可移动的四条线
  107. //向外扩展3像素,这样可以使方块组到达边界的时候再移动就会发生碰撞
  108. m_topLine = m_scene->addLine(32,32,238,32);
  109. m_leftLine = m_scene->addLine(32,32,32,438);
  110. m_buttomLine = m_scene->addLine(32,438,238,438);
  111. m_rightLine = m_scene->addLine(238,32,238,438);
  112. m_topLine->setPen(QPen(QColor(255,255,255)));
  113. m_leftLine->setPen(QPen(QColor(255,255,255)));
  114. m_buttomLine->setPen(QPen(QColor(255,255,255)));
  115. m_rightLine->setPen(QPen(QColor(255,255,255)));
  116. m_currentBox = new pieceBox;
  117. m_nextBox = new pieceBox;
  118. connect(m_currentBox,SIGNAL(signal_needNewBox()),this,SLOT(slot_clearFullRows()));
  119. connect(m_currentBox,SIGNAL(signal_gameOver()),this,SLOT(slot_gameOver()));
  120. m_scene->addItem(m_currentBox);
  121. m_scene->addItem(m_nextBox);
  122. }
  123. void Panel::startGame()
  124. {
  125. m_currentBox->createBox(QPointF(135,55));
  126. m_nextBox->createBox(QPointF(290,55));
  127. //将键盘焦点给当前的方块组
  128. m_currentBox->setFocus();
  129. m_currentBox->slot_startTimer(m_currentLVSpeed);
  130. }
  131. void Panel::updateScore(const int fullRows)
  132. {
  133. int score = ui->LCDNum_Score->value() + fullRows*ROWSCORE;
  134. ui->LCDNum_Score->display(score);
  135. if(score < 1000) //第一关
  136. {
  137. }else if(score < 2000) //第二关
  138. {
  139. setbackground(LV2);
  140. initControlWidget();
  141. m_currentBox->slot_stopTimer();
  142. m_currentBox->slot_startTimer(m_currentLVSpeed);
  143. }else if(score < 4000) //第三关
  144. {
  145. setbackground(LV3);
  146. initControlWidget();
  147. m_currentBox->slot_stopTimer();
  148. m_currentBox->slot_startTimer(m_currentLVSpeed);
  149. }else if(score < 8000) //第四关
  150. {
  151. setbackground(LV4);
  152. initControlWidget();
  153. m_currentBox->slot_stopTimer();
  154. m_currentBox->slot_startTimer(m_currentLVSpeed);
  155. }else if(score < 16000) //第五关
  156. {
  157. setbackground(LV5);
  158. initControlWidget();
  159. m_currentBox->slot_stopTimer();
  160. m_currentBox->slot_startTimer(m_currentLVSpeed);
  161. }else //从第一关重新开始
  162. {
  163. setbackground(LV1);
  164. initControlWidget();
  165. m_currentBox->slot_stopTimer();
  166. m_currentBox->slot_startTimer(m_currentLVSpeed);
  167. }
  168. }
  169. /**
  170. * 开始游戏
  171. * @brief Panel::on_pbt_startGame_clicked
  172. */
  173. void Panel::on_pbt_startGame_clicked()
  174. {
  175. m_isGameOver = false;
  176. if(m_isPauseGame)
  177. {
  178. ui->pbt_startGame->setEnabled(false);
  179. m_isPauseGame = false;
  180. m_currentBox->slot_startTimer(m_currentLVSpeed);
  181. return;
  182. }
  183. //默认等级为LV1
  184. setbackground(LV1);
  185. initControlWidget();
  186. startGame();
  187. }
  188. /**
  189. * 暂停游戏
  190. * @brief Panel::on_pbt_pauseGame_clicked
  191. */
  192. void Panel::on_pbt_pauseGame_clicked()
  193. {
  194. if(m_isPauseGame)
  195. {
  196. return;
  197. }
  198. m_isPauseGame = true;
  199. m_currentBox->slot_stopTimer();
  200. ui->pbt_startGame->setEnabled(true);
  201. QMessageBox::information(this,"提示","暂停游戏!",QMessageBox::Yes);
  202. }
  203. /**
  204. * 重新开始游戏
  205. * @brief Panel::on_pbt_restart_clicked
  206. */
  207. void Panel::on_pbt_restart_clicked()
  208. {
  209. m_currentBox->slot_stopTimer();
  210. m_currentBox->clearBoxGroup();
  211. m_nextBox->clearBoxGroup(true);
  212. //先将当前的小正方形组移出游戏框,防止下面的清除item将该方块组清除了
  213. m_currentBox->setPos(290,55);
  214. ui->LCDNum_Score->display(0);
  215. //清空视图中所有的小方块
  216. foreach (QGraphicsItem *item, m_scene->items(34, 34, 204, 404, Qt::ContainsItemShape,Qt::AscendingOrder)) {
  217. // 先从场景中移除小方块,因为使用deleteLater()是在返回主事件循环后才销毁
  218. // 小方块的,为了在出现新的方块组时不发生碰撞,所以需要先从场景中移除小方块
  219. m_scene->removeItem(item);
  220. onePiece *piece = (onePiece*) item;
  221. piece->deleteLater();
  222. }
  223. m_isPauseGame = false;
  224. on_pbt_startGame_clicked();
  225. }
  226. /**
  227. * 停止游戏
  228. * @brief Panel::on_pbt_stopGame_clicked
  229. */
  230. void Panel::on_pbt_stopGame_clicked()
  231. {
  232. m_currentBox->slot_stopTimer();
  233. slot_gameOver();
  234. }
  235. /**清除满行的小方块
  236. * @brief Panel::slot_clearFullRows
  237. */
  238. void Panel::slot_clearFullRows()
  239. {
  240. m_rowList.clear();
  241. //获取比一行方格多行的所有小方块,不包含最高的一行
  242. for(int i = 414;i > 35; i-=20)
  243. {
  244. //返回可视区域(202*22)内所有的完全可视的item
  245. QList<QGraphicsItem *> itemList = m_scene->items(34,i,204,22,Qt::ContainsItemShape,Qt::AscendingOrder);
  246. // qDebug() << "可是区域内的item数:" << itemList.count();
  247. //已满
  248. if(itemList.count() == 10)
  249. {
  250. //遍历列表删除小方块
  251. foreach (QGraphicsItem *item, itemList) {
  252. onePiece *piece = (onePiece *)item;
  253. //模糊效果,先放大再缩小
  254. QGraphicsBlurEffect *blurEffect = new QGraphicsBlurEffect;
  255. piece->setGraphicsEffect(blurEffect);
  256. QPropertyAnimation *animation = new QPropertyAnimation(piece,"scale");
  257. animation->setDuration(250);
  258. animation->setEasingCurve(QEasingCurve::OutBounce);
  259. animation->setStartValue(4);
  260. animation->setEndValue(0.25);
  261. animation->start(QAbstractAnimation::DeleteWhenStopped);
  262. connect(animation,SIGNAL(finished()),piece,SLOT(deleteLater()));
  263. }
  264. m_mediaPlayer->play();
  265. //记录满行的行地址
  266. m_rowList.append(i);
  267. }
  268. }
  269. // qDebug() << "满行的行数:" << m_rowList.size();
  270. //存在满行,删除后将上面的方块下移
  271. if(!m_rowList.isEmpty())
  272. {
  273. //等待所有的小正方形都销毁后再将上方的小正方形向下移动
  274. QTimer::singleShot(300,this,SLOT(slot_moveBox()));
  275. }else //直接创建新的方块组
  276. {
  277. m_currentBox->createBox(QPointF(135,55), m_nextBox->getCurrentBoxType());
  278. // 清空并销毁提示方块组中的所有小方块
  279. m_nextBox->clearBoxGroup(true);
  280. if(!m_isGameOver)
  281. {
  282. m_nextBox->createBox(QPointF(290,55));
  283. }
  284. }
  285. }
  286. void Panel::slot_gameOver()
  287. {
  288. // qDebug() << "game over";
  289. m_isGameOver = true;
  290. QMessageBox::information(this,"提示",QString("您的游戏得分为%1!").arg(ui->LCDNum_Score->value()),QMessageBox::Yes);
  291. ui->LCDNum_Score->display(0);
  292. m_currentBox->clearFocus();
  293. setbackground(LV0);
  294. initControlWidget();
  295. m_isPauseGame = false;
  296. //初始化界面
  297. m_currentBox->clearBoxGroup(true);
  298. m_nextBox->clearBoxGroup(true);
  299. //先将当前的小正方形组移出游戏框,防止下面的清除item将该方块组清除了
  300. m_currentBox->setPos(290,55);
  301. //清空视图中所有的小方块
  302. foreach (QGraphicsItem *item, m_scene->items(34, 34, 204, 404, Qt::ContainsItemShape,Qt::AscendingOrder)) {
  303. // 先从场景中移除小方块,因为使用deleteLater()是在返回主事件循环后才销毁
  304. // 小方块的,为了在出现新的方块组时不发生碰撞,所以需要先从场景中移除小方块
  305. m_scene->removeItem(item);
  306. onePiece *piece = (onePiece*) item;
  307. piece->deleteLater();
  308. }
  309. }
  310. /**清空满行的小方块后向下移动上面的小方块
  311. * @brief Panel::slot_moveBox
  312. */
  313. void Panel::slot_moveBox()
  314. {
  315. // 从位置最靠上的满行开始
  316. for (int i = m_rowList.count(); i > 0; i--)
  317. {
  318. foreach (QGraphicsItem *item, m_scene->items(34, 34, 206, m_rowList.at(i-1) - 32, Qt::ContainsItemShape,Qt::AscendingOrder)) {
  319. item->moveBy(0, 20);
  320. }
  321. }
  322. // 更新分数
  323. updateScore(m_rowList.count());
  324. // 将满行列表清空为0
  325. m_rowList.clear();
  326. // 等所有行下移以后再出现新的方块组
  327. m_currentBox->createBox(QPointF(135,55), m_nextBox->getCurrentBoxType());
  328. // 清空并销毁提示方块组中的所有小方块
  329. m_nextBox->clearBoxGroup(true);
  330. m_nextBox->createBox(QPointF(290,55));
  331. }
  • 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
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 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
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362

http://blog.csdn.net/sxpsxp12/article/details/50607224

Qt5图形视图框架的“俄罗斯方块”(使用了QGraphicsView)的更多相关文章

  1. Qt图形视图框架公开课资料

    接受CSDN学院的邀请,讲一次公开课,主题是Qt图形视图框架,报名链接在这里:http://edu.csdn.net/huiyiCourse/detail/228. 内容有两部分:自定义Item和拖放 ...

  2. Qt之图形视图框架

    简述 图形视图(Graphics View)提供了一个平台,用于大量自定义2D图元的管理与交互,并提供了一个视图部件(view widget)来显示可以缩放和旋转的图元. 框架包括一个事件传播架构,支 ...

  3. 用Qt图形视图框架开发拼图游戏

    用Qt的图形视图框架(Graphics View Framework)做了一个拼图游戏DEMO,演示了: QGraphicsView.QGraphicsScene.QGraphicsItem的基本用法 ...

  4. Qt-MVC图形视图框架分解

    前面在<Qt-MVC图形视图框架出识>中我们了解了Qt图形视图框架中三个最基本的类,弄清他们的关系,本片小文,我们将对QGraphicsView,QGraphiceScene,QGraph ...

  5. Qt-MVC图形视图框架初识

    使用QPushButton.QLabel.QCheckBox等构成GUI的控件或自定义图形时,开发应用程序会变得很简单.但是如果想在GUI中使用数十个或者数百个图形对象,向用户完美展示控制场景,则会受 ...

  6. Qt 2D绘图之六:图形视图框架的事件处理与传播

    一.简介 图形视图框架中的事件都是首先由视图进行接收,然后传递给场景,再由场景传递给相应的图形项.而对于键盘事件,它会传递给获得焦点的图形项,可以使用QGraphicsScene类的setFocusI ...

  7. Qt 2D绘图之五:图形视图框架的结构和坐标系统

    一.图形视图框架的结构 在前面讲的基本绘图中,我们可以自己绘制各种图形,并且控制它们.但是,如果需要同时绘制很多个相同或不同的图形,并且要控制它们的移动.检测它们的碰撞和叠加:或者我们想让自己绘制的图 ...

  8. QT 图形视图框架

    https://blog.csdn.net/qq769651718/article/details/79357936 使用QPushButton.QLabel.QCheckBox等构成GUI的控件或自 ...

  9. Qt开发技术:图形视图框架(一)基本介绍

    前话   使用到Qt的视图框架.   Qt视图框架介绍 简介   图形视图框架(The Graphic View Framework)用于管理和与大量定制的二维图形项目交互,以及用于可视化项目的视图小 ...

随机推荐

  1. 改变浏览器中默认的ctrl+s方法

    在一般的情况下,我们在浏览网页的时候按下ctrl+s,浏览器会弹出一个保存网页的框. 但是在一些特定的网页中,我们希望ctrl+s不是弹出默认的保存窗口,而是进行一下别的操作. 比如在我们使用简书的时 ...

  2. win10下安装docker步骤(一)

    一.启用Hyper-V 打开控制面板 - 程序和功能 - 启用或关闭Windows功能,勾选Hyper-V,然后点击确定即可,如图: 请注意电脑默认的Hyper-V虚拟机监控程序是不能进行勾选的,需要 ...

  3. Smarty优缺点

    Smarty优点: 1. 速度:采用smarty编写的程序可以获得最大速度的提高,这一点是相对于其它的模板引擎技术而言的. 2. 编译型:采用smarty编写的程序在运行时要编译成一个非模板技术的PH ...

  4. twemproxy分片处理原理--剖析twemproxy代码正编

    twemproxy在redis上能处理多命令流程只有mset,mget,del的命令,例如mset的话是mset k1 v1 k2 v2 k3 k3,mget的话是mget k1 k2 k3,del的 ...

  5. C#或者WPF中让某个窗体置顶

    原文:C#或者WPF中让某个窗体置顶 前记:在工作中有个需求,要求不管到那个界面,我必须让一个浮动条(其实是个窗体)置顶. 我用wpf,因为有之前有好几个界面已经设置成topmost了,所以在这几个界 ...

  6. Leetcode 319 Bulb Switcher 找规律

    有n盏关着的灯,第k轮把序号为k倍数的关着的灯打开,开着的灯关闭. class Solution { public: int bulbSwitch(int n) { return (int)sqrt( ...

  7. matlab 求解 Ax=B 时所用算法

    x = A\B; x = mldivide(A, B); matlab 在这里的求解与严格的数学意义是不同的, 如果 A 接近奇异,matlab 仍会给出合理的结果,但也会提示警告信息: 如果 A 为 ...

  8. html 自制属性

    HTML5 允许扩展的(自制的)属性,以 data- 开头 <label id="id0" data-value="0">0</label&g ...

  9. 一个由单例模式在多线程环境下引发的 bug

    问题症状 HTTP 日志系统,老是出现日志信息覆盖的情况.比如同时调用 A 接口和 B 接口,B 接口请求响应信息变成了 A 接口请求响应相关信息.这个问题在并发量大的情况下越来越严重. 问题初步分析 ...

  10. Qt MVC设计模式(五篇)

    http://blog.csdn.net/qq_19672579/article/details/46952675http://blog.csdn.net/qq_19672579/article/de ...