QT开发(四十)——GraphicsView编程
来源:互联网 发布:天池 大数据 编辑:程序博客网 时间:2024/05/21 16:59
一、QGraphicsScene
1、QGraphicsScene
2、事件处理与传播
3、索引算法
4、边界矩形
5、图元的查找
二、QGraphicsItem
1、自定义QGraphicsItem
2、绘制
3、排序
4、事件处理
5、动画效果
6、移动
7、图元的坐标转换
8、主要成员函数
三、QGraphicsView
1、QGraphicsView简介
2、事件处理
四、程序实例
1、自定义视图
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef CUSTOMVIEW_H
#define CUSTOMVIEW_H
#include <QGraphicsView>
class
CustomView :
public
QGraphicsView
{
Q_OBJECT
public
:
CustomView(QWidget *parent = 0);
protected
:
void
mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
void
mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
void
mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
void
paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
void
dragEnterEvent(QDragEnterEvent *event) Q_DECL_OVERRIDE;
void
dragLeaveEvent(QDragLeaveEvent *event) Q_DECL_OVERRIDE;
void
dragMoveEvent(QDragMoveEvent *event) Q_DECL_OVERRIDE;
void
dropEvent(QDropEvent *event) Q_DECL_OVERRIDE;
};
#endif // CUSTOMVIEW_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
#include "CustomView.h"
#include <QDebug>
CustomView::CustomView(QWidget *parent):QGraphicsView(parent)
{
}
void
CustomView::mousePressEvent(QMouseEvent *event)
{
qDebug() <<
"CustomView::mousePressEvent"
;
QGraphicsView::mousePressEvent(event);
}
void
CustomView::mouseMoveEvent(QMouseEvent *event)
{
qDebug() <<
"CustomView::mouseMoveEvent"
;
QGraphicsView::mouseMoveEvent(event);
}
void
CustomView::mouseReleaseEvent(QMouseEvent *event)
{
qDebug() <<
"CustomView::mouseReleaseEvent"
;
QGraphicsView::mouseReleaseEvent(event);
}
void
CustomView::paintEvent(QPaintEvent *event)
{
qDebug() <<
"CustomView::paintEvent"
;
QGraphicsView::paintEvent(event);
}
void
CustomView::dragEnterEvent(QDragEnterEvent *event)
{
qDebug() <<
"CustomView::dragEnterEvent"
;
QGraphicsView::dragEnterEvent(event);
}
void
CustomView::dragLeaveEvent(QDragLeaveEvent *event)
{
qDebug() <<
"CustomView::dragLeaveEvent"
;
QGraphicsView::dragLeaveEvent(event);
}
void
CustomView::dragMoveEvent(QDragMoveEvent *event)
{
setCursor(Qt::CrossCursor);
qDebug() <<
"CustomView::dragMoveEvent"
;
QGraphicsView::dragMoveEvent(event);
}
void
CustomView::dropEvent(QDropEvent *event)
{
qDebug() <<
"CustomView::dropEvent"
;
QGraphicsView::dropEvent(event);
}
2、自定义场景
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef CUSTOMSCENE_H
#define CUSTOMSCENE_H
#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QPaintEvent>
class
CustomScene :
public
QGraphicsScene
{
Q_OBJECT
public
:
CustomScene(QObject *parent = 0);
protected
:
void
mousePressEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE;
void
mouseReleaseEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE;
void
mouseMoveEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE;
void
dragEnterEvent(QGraphicsSceneDragDropEvent *event) Q_DECL_OVERRIDE;
void
dragLeaveEvent(QGraphicsSceneDragDropEvent *event) Q_DECL_OVERRIDE;
void
dragMoveEvent(QGraphicsSceneDragDropEvent *event) Q_DECL_OVERRIDE;
void
dropEvent(QGraphicsSceneDragDropEvent *event) Q_DECL_OVERRIDE;
};
#endif // CUSTOMSCENE_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
#include "CustomScene.h"
#include <QDebug>
CustomScene::CustomScene(QObject *parent):QGraphicsScene(parent)
{
}
void
CustomScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
qDebug() <<
"CustomScene::mousePressEvent"
;
QGraphicsScene::mousePressEvent(event);
}
void
CustomScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
qDebug() <<
"CustomScene::mouseReleaseEvent"
;
QGraphicsScene::mouseReleaseEvent(event);
}
void
CustomScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
qDebug() <<
"CustomScene::mouseMoveEvent"
;
QGraphicsScene::mouseMoveEvent(event);
}
void
CustomScene::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
{
qDebug() <<
"CustomScene::dragEnterEvent"
;
QGraphicsScene::dragEnterEvent(event);
}
void
CustomScene::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
{
qDebug() <<
"CustomScene::dragLeaveEvent"
;
QGraphicsScene::dragLeaveEvent(event);
}
void
CustomScene::dragMoveEvent(QGraphicsSceneDragDropEvent *event)
{
qDebug() <<
"CustomScene::dragMoveEvent"
;
QGraphicsScene::dragMoveEvent(event);
}
void
CustomScene::dropEvent(QGraphicsSceneDragDropEvent *event)
{
qDebug() <<
"CustomScene::dropEvent"
;
QGraphicsScene::dropEvent(event);
}
3、自定义图元
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
#ifndef CUSTOMITEM_H
#define CUSTOMITEM_H
#include <QGraphicsItem>
#include <QGraphicsSceneMouseEvent>
class
CustomItem :
public
QGraphicsItem
{
public
:
CustomItem();
void
paint(QPainter *painter,
const
QStyleOptionGraphicsItem *option, QWidget *widget) Q_DECL_OVERRIDE;
QRectF boundingRect()
const
Q_DECL_OVERRIDE;
protected
:
//鼠标事件
void
mousePressEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE;
void
mouseMoveEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE;
void
mouseReleaseEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE;
//拖拽事件
void
dragEnterEvent(QGraphicsSceneDragDropEvent *event) Q_DECL_OVERRIDE;
void
dragLeaveEvent(QGraphicsSceneDragDropEvent *event) Q_DECL_OVERRIDE;
void
dragMoveEvent(QGraphicsSceneDragDropEvent *event) Q_DECL_OVERRIDE;
void
dropEvent(QGraphicsSceneDragDropEvent *event) Q_DECL_OVERRIDE;
private
:
QColor color;
};
#endif // CUSTOMITEM_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
75
76
77
#include "CustomItem.h"
#include <QDebug>
#include <QPainter>
#include <QCursor>
#include <QPen>
CustomItem::CustomItem()
{
color = Qt::red;
setFlag(QGraphicsItem::ItemIsFocusable);
//设置图元为可移动的
setFlag(QGraphicsItem::ItemIsMovable);
setAcceptDrops(
true
);
}
void
CustomItem::paint(QPainter *painter,
const
QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
qDebug() <<
"CustomItem::paint"
;
if
(hasFocus()) {
painter->setPen(QPen(QColor(255,255,255,200)));
}
else
{
painter->setPen(QPen(QColor(100,100,100,100)));
}
painter->setBrush(color);
painter->drawRect(-10, -10, 20, 20);
}
QRectF CustomItem::boundingRect()
const
{
qreal adjust = 0.5;
return
QRectF(-10 - adjust, -10 - adjust, 20 + adjust, 20 + adjust);
}
void
CustomItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
Q_UNUSED(event);
qDebug() <<
"CustomItem::mousePressEvent"
;
setCursor(Qt::OpenHandCursor);
}
void
CustomItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
setCursor(Qt::DragMoveCursor);
qDebug() <<
"CustomItem::mouseMoveEvent"
;
}
void
CustomItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
qDebug() <<
"CustomItem::mouseReleaseEvent"
;
setCursor(Qt::ArrowCursor);
}
void
CustomItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
{
setCursor(Qt::CrossCursor);
qDebug() <<
"CustomItem::dragEnterEvent"
;
}
void
CustomItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
{
setCursor(Qt::ForbiddenCursor);
qDebug() <<
"CustomItem::dragLeaveEvent"
;
}
void
CustomItem::dragMoveEvent(QGraphicsSceneDragDropEvent *event)
{
setCursor(Qt::CrossCursor);
qDebug() <<
"CustomItem::dragMoveEvent"
;
}
void
CustomItem::dropEvent(QGraphicsSceneDragDropEvent *event)
{
setCursor(Qt::WaitCursor);
qDebug() <<
"CustomItem::dropEvent"
;
}
4、程序使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "CustomScene.h"
#include "CustomView.h"
#include "CustomItem.h"
#include <QApplication>
#include <QTime>
int
main(
int
argc,
char
*argv[])
{
QApplication a(argc, argv);
qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
CustomScene scene;
scene.setSceneRect(-200, -150, 400, 300);
for
(
int
i = 0; i < 5; ++i) {
CustomItem *item =
new
CustomItem;
item->setPos(i * 50 - 90, -50);
scene.addItem(item);
}
CustomView view;
view.setScene(&scene);
view.show();
return
a.exec();
}
阅读全文
0 0
- QT开发(四十)——GraphicsView编程
- QT开发(四十)——GraphicsView编程
- QT开发(三十九)——GraphicsView框架
- QT开发(三十九)——GraphicsView框架
- QT开发(四十七)——数据库驱动层
- QT开发(四十三)——SAX方式解析XML
- Qt的GraphicsView框架
- Qt浅谈之十七:飞舞的蝴蝶(GraphicsView框架)
- Qt浅谈之十七:飞舞的蝴蝶(GraphicsView框架)
- Qt浅谈之十七:飞舞的蝴蝶(GraphicsView框架)
- Qt浅谈之十七:飞舞的蝴蝶(GraphicsView框架)
- Qt浅谈之十七:飞舞的蝴蝶(GraphicsView框架)
- QT开发(四十六)——QT数据库编程基础
- QT开发(五十)——QT串口编程基础
- QT开发(三十四)——QT多线程编程
- QT开发(三十八)——Model/View框架编程
- QT之数据库 和 GraphicsView使用
- qt:graphicsitem graphicsscene graphicsview坐标关系实例
- Codeforces 432 Div. 2-D-Arpa and a list of numbers(枚举倍数求GCD)
- 在Andorid中使用FFmpeg实现YUV编码为MP4
- python操作docx文档
- AJAX第五步:使用ajax解决页面首页显示后台数据的问题
- struts2添加自己的校验器
- QT开发(四十)——GraphicsView编程
- 动态查看最近300行的代码
- 机器学习-- 逻辑回归
- Hdu 5784 How Many Triangles(极角排序+尺取法)
- JS常见面试题
- java文件读取
- C++ Prime Plus第六版--第二章复习题
- POJ1077 Eight —— 反向BFS
- jQuery的初步使用