Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
mahuifa
QMDemo
提交
8b053631
Q
QMDemo
项目概览
mahuifa
/
QMDemo
通知
1
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Q
QMDemo
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
8b053631
编写于
9月 02, 2022
作者:
mahuifa
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
注释:为项目添加必要注释
上级
43e45df6
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
66 addition
and
4 deletion
+66
-4
GraphicsviewDemo/GraphicsItem/GraphicsItem.pro
GraphicsviewDemo/GraphicsItem/GraphicsItem.pro
+14
-0
GraphicsviewDemo/GraphicsItem/mainwindow.cpp
GraphicsviewDemo/GraphicsItem/mainwindow.cpp
+3
-1
GraphicsviewDemo/GraphicsItem/qgraphicspointsitem.cpp
GraphicsviewDemo/GraphicsItem/qgraphicspointsitem.cpp
+39
-2
GraphicsviewDemo/GraphicsItem/qgraphicspointsitem.h
GraphicsviewDemo/GraphicsItem/qgraphicspointsitem.h
+9
-0
GraphicsviewDemo/GraphicsviewDemo.pro
GraphicsviewDemo/GraphicsviewDemo.pro
+1
-1
未找到文件。
GraphicsviewDemo/GraphicsItem/GraphicsItem.pro
浏览文件 @
8b053631
#---------------------------------------------------------------------------------------
#
@
功能:
QGraphicsItem
基本图元演示
#
1
、包含内置的
直线、矩形、椭圆、多边形、简单文本、富文本、图片、绘图路径、窗口部件基本图元;
#
2
、使用自定义散点图元;
#
3
、实现所有图元鼠标选中、移动功能,包括窗口部件图元;
#
4
、解决自定义图元鼠标移动存在残留问题。
#
@
编译器:
Desktop
Qt
5.12
.
5
MSVC2017
64
bit
(也支持其它编译器)
#
@
Qt
IDE
:
D
:/
Qt
/
Qt5
.
12.5
/
Tools
/
QtCreator
/
share
/
qtcreator
#
#
@
开发者
mhf
#
@
邮箱
1603291350
@
qq
.
com
#
@
时间
2022
-
09
-
02
23
:
17
:
22
#
@
备注
#---------------------------------------------------------------------------------------
QT
+=
core
gui
greaterThan
(
QT_MAJOR_VERSION
,
4
)
:
QT
+=
widgets
...
...
GraphicsviewDemo/GraphicsItem/mainwindow.cpp
浏览文件 @
8b053631
...
...
@@ -17,6 +17,8 @@ MainWindow::MainWindow(QWidget *parent)
{
ui
->
setupUi
(
this
);
this
->
setWindowTitle
(
"QGraphicsItem基本图元演示"
);
initToolBar
();
ui
->
graphicsView
->
setScene
(
&
m_scene
);
// 添加绘图场景
...
...
@@ -38,7 +40,7 @@ void MainWindow::initToolBar()
QPushButton
*
but3
=
new
QPushButton
(
"椭圆"
);
QPushButton
*
but4
=
new
QPushButton
(
"多边形"
);
QPushButton
*
but5
=
new
QPushButton
(
"简单文本"
);
QPushButton
*
but6
=
new
QPushButton
(
"文本"
);
QPushButton
*
but6
=
new
QPushButton
(
"
富
文本"
);
QPushButton
*
but7
=
new
QPushButton
(
"图片"
);
QPushButton
*
but8
=
new
QPushButton
(
"路径"
);
QPushButton
*
but9
=
new
QPushButton
(
"窗口"
);
...
...
GraphicsviewDemo/GraphicsItem/qgraphicspointsitem.cpp
浏览文件 @
8b053631
...
...
@@ -14,25 +14,41 @@ QGraphicsPointsItem::~QGraphicsPointsItem()
}
/**
* @brief 返回绘制的画笔对象
* @return
*/
QPen
QGraphicsPointsItem
::
pen
()
const
{
return
m_pen
;
}
/**
* @brief 设置画笔对象
* @param pen
*/
void
QGraphicsPointsItem
::
setPen
(
const
QPen
&
pen
)
{
if
(
m_pen
==
pen
)
return
;
prepareGeometryChange
();
prepareGeometryChange
();
// 准备几何图形更改,以使QGraphicsScene的索引保持最新
m_pen
=
pen
;
update
();
}
/**
* @brief 返回用于绘制的点数据
* @return
*/
QPolygonF
QGraphicsPointsItem
::
points
()
const
{
return
m_points
;
}
/**
* @brief 设置用于绘制的散点数据
* @param points
*/
void
QGraphicsPointsItem
::
setPoints
(
const
QPolygonF
&
points
)
{
if
(
m_points
==
points
)
...
...
@@ -53,10 +69,25 @@ QRectF QGraphicsPointsItem::boundingRect() const
return
m_points
.
boundingRect
().
adjusted
(
-
pad
,
-
pad
,
pad
,
pad
);
}
/**
* @brief 判断是否包含传入的点
* @param point 传入数据点
* @return 包含返回true,否则返回false
*/
bool
QGraphicsPointsItem
::
contains
(
const
QPointF
&
point
)
const
{
#if 1 // 用这种方法只有QPolygonF中有的点才返回true
for
(
auto
pointf
:
m_points
)
{
if
(
pointf
==
point
)
{
return
true
;
}
}
return
false
;
#else // 用这种方法只要包含在当前图元边框矩形内的点都返回true
return
QGraphicsItem
::
contains
(
point
);
#endif
}
/**
...
...
@@ -90,6 +121,12 @@ static void qt_graphicsItem_highlightSelected(
painter
->
drawRect
(
item
->
boundingRect
().
adjusted
(
pad
,
pad
,
-
pad
,
-
pad
));
}
/**
* @brief 重写这个函数进行自定义图案绘制
* @param painter 用于绘制的画家对象
* @param option QStyleOptionsGraphicsItem类用于描述绘制QGraphicsItem所需的参数。
* @param widget
*/
void
QGraphicsPointsItem
::
paint
(
QPainter
*
painter
,
const
QStyleOptionGraphicsItem
*
option
,
QWidget
*
widget
)
{
Q_UNUSED
(
widget
)
...
...
GraphicsviewDemo/GraphicsItem/qgraphicspointsitem.h
浏览文件 @
8b053631
/******************************************************************************
* @文件名 qgraphicspointsitem.h
* @功能 自定义散点图元,解决了鼠标移动残留问题
*
* @开发者 mhf
* @邮箱 1603291350@qq.com
* @时间 2022/09/02
* @备注
*****************************************************************************/
#ifndef QGRAPHICSPOINTSITEM_H
#define QGRAPHICSPOINTSITEM_H
...
...
GraphicsviewDemo/GraphicsviewDemo.pro
浏览文件 @
8b053631
TEMPLATE
=
subdirs
SUBDIRS
+=
GraphicsItem
#
图元项
Demo
SUBDIRS
+=
GraphicsItem
#
QGraphicsItem
基本图元演示
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录