Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
mahuifa
QMDemo
提交
8b1b11ff
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,发现更多精彩内容 >>
提交
8b1b11ff
编写于
7月 21, 2022
作者:
mahuifa
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
新功能:Transformations中增加renderarea类
1、实现renderarea类中绘制虚线框、缩放、选择、偏移坐标轴、绘制坐标轴功能。
上级
9a744524
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
169 addition
and
0 deletion
+169
-0
PaintingDemo/Transformations/Transformations.pro
PaintingDemo/Transformations/Transformations.pro
+2
-0
PaintingDemo/Transformations/renderarea.cpp
PaintingDemo/Transformations/renderarea.cpp
+101
-0
PaintingDemo/Transformations/renderarea.h
PaintingDemo/Transformations/renderarea.h
+34
-0
PaintingDemo/Transformations/widget.cpp
PaintingDemo/Transformations/widget.cpp
+29
-0
PaintingDemo/Transformations/widget.h
PaintingDemo/Transformations/widget.h
+3
-0
未找到文件。
PaintingDemo/Transformations/Transformations.pro
浏览文件 @
8b1b11ff
...
...
@@ -27,9 +27,11 @@ DEFINES += QT_DEPRECATED_WARNINGS
SOURCES
+=
\
main
.
cpp
\
renderarea
.
cpp
\
widget
.
cpp
HEADERS
+=
\
renderarea
.
h
\
widget
.
h
#
Default
rules
for
deployment
.
...
...
PaintingDemo/Transformations/renderarea.cpp
0 → 100644
浏览文件 @
8b1b11ff
#include "renderarea.h"
#include <QDebug>
#include <qpainter.h>
RenderArea
::
RenderArea
(
QWidget
*
parent
)
:
QWidget
(
parent
)
{
this
->
setMinimumSize
(
200
,
200
);
// 设置背景填充色
this
->
setBackgroundRole
(
QPalette
::
Base
);
this
->
setAutoFillBackground
(
true
);
QFont
newFont
=
this
->
font
();
newFont
.
setPixelSize
(
20
);
// 设置字体大小
this
->
setFont
(
newFont
);
// 设置当前类使用的字体
QFontMetrics
fontMetrics
(
newFont
);
// QFontMetrics 类提供字体度量信息。
m_fontRectX
=
fontMetrics
.
boundingRect
(
"X"
);
// 返回由 text 指定的字符串中字符的边界矩形
m_fontRectY
=
fontMetrics
.
boundingRect
(
"Y"
);
}
/**
* @brief 重绘事件函数
* @param event
*/
void
RenderArea
::
paintEvent
(
QPaintEvent
*
event
)
{
QWidget
::
paintEvent
(
event
);
QPainter
painter
(
this
);
// 创建一个画家对象
painter
.
setRenderHint
(
QPainter
::
Antialiasing
);
// 设置抗锯齿
painter
.
translate
(
60
,
60
);
// 坐标原点偏移到(60, 60)位置
drawOutline
(
painter
);
// 画一个虚线矩形框
painter
.
save
();
// 保存当前绘制状态,save()后所有QPainter设置在restore()后将失效(比如画笔设置)
transformPainter
(
painter
);
// 设置偏移、缩放、旋转
drawCoordinates
(
painter
);
// 绘制坐标系
painter
.
restore
();
}
/**
* @brief 画一个虚线矩形框
* @param painter
*/
void
RenderArea
::
drawOutline
(
QPainter
&
painter
)
{
painter
.
save
();
// 将QPainter状态保存,后面的设置不会产生影响
painter
.
setPen
(
Qt
::
darkBlue
);
// 设置画笔颜色
painter
.
setPen
(
Qt
::
DashLine
);
// 画笔样式为虚线
painter
.
drawRect
(
0
,
0
,
100
,
100
);
// 画虚线矩形
painter
.
restore
();
}
/**
* @brief 在这个函数中设置画家对象的坐标原点偏移、旋转、缩放
* @param painter
*/
void
RenderArea
::
transformPainter
(
QPainter
&
painter
)
{
for
(
auto
operation
:
operations
)
{
switch
(
operation
)
{
case
Translate
:
painter
.
translate
(
50
,
50
);
// 坐标原点偏移 50 像素
break
;
case
Scale
:
painter
.
scale
(
0.75
,
0.75
);
// 缩小到原来的75%大小
break
;
case
Rotate
:
painter
.
rotate
(
60
);
// 顺时针旋转60度
break
;
default:
break
;
}
}
}
/**
* @brief 绘制当前画图使用的坐标系
* @param painter
*/
void
RenderArea
::
drawCoordinates
(
QPainter
&
painter
)
{
painter
.
save
();
painter
.
setPen
(
Qt
::
red
);
painter
.
drawLine
(
0
,
0
,
50
,
0
);
// 绘制X轴直线
painter
.
drawLine
(
45
,
-
3
,
50
,
0
);
// 画X轴箭头
painter
.
drawLine
(
45
,
3
,
50
,
0
);
painter
.
drawText
(
QRect
(
50
,
0
,
m_fontRectX
.
width
(),
-
m_fontRectX
.
height
()),
Qt
::
AlignCenter
,
"X"
);
painter
.
drawLine
(
0
,
0
,
0
,
50
);
// 绘制Y轴
painter
.
drawLine
(
3
,
45
,
0
,
50
);
// 画Y轴箭头
painter
.
drawLine
(
-
3
,
45
,
0
,
50
);
painter
.
drawText
(
QRect
(
0
,
50
,
-
m_fontRectX
.
width
(),
m_fontRectX
.
height
()),
Qt
::
AlignCenter
,
"Y"
);
painter
.
restore
();
}
PaintingDemo/Transformations/renderarea.h
0 → 100644
浏览文件 @
8b1b11ff
#ifndef RENDERAREA_H
#define RENDERAREA_H
#include <QWidget>
// 绘图操作类型
enum
Operation
{
NoOperation
,
// 没有进行操作
Translate
,
// 偏移绘图坐标原点
Rotate
,
// 以坐标原点为中心进行旋转
Scale
// 将绘制图案按比例缩放
};
class
RenderArea
:
public
QWidget
{
Q_OBJECT
public:
explicit
RenderArea
(
QWidget
*
parent
=
nullptr
);
protected:
void
paintEvent
(
QPaintEvent
*
event
)
override
;
void
drawOutline
(
QPainter
&
painter
);
void
transformPainter
(
QPainter
&
painter
);
void
drawCoordinates
(
QPainter
&
painter
);
public
slots
:
private:
QList
<
Operation
>
operations
;
// 保存所有的缩放、偏移、旋转操作
QRect
m_fontRectX
;
// X轴字体矩形范围
QRect
m_fontRectY
;
// Y轴字体矩形范围
};
#endif // RENDERAREA_H
PaintingDemo/Transformations/widget.cpp
浏览文件 @
8b1b11ff
#include "widget.h"
#include "renderarea.h"
#include <qcombobox.h>
#include <qgridlayout.h>
Widget
::
Widget
(
QWidget
*
parent
)
:
QWidget
(
parent
)
{
initUI
();
}
Widget
::~
Widget
()
...
...
@@ -10,6 +15,30 @@ Widget::~Widget()
void
Widget
::
initUI
()
{
m_originalRenderArea
=
new
RenderArea
(
this
);
m_comShape
=
new
QComboBox
(
this
);
m_comShape
->
addItem
(
"时钟图案"
);
m_comShape
->
addItem
(
"房子图案"
);
m_comShape
->
addItem
(
"文字图案"
);
m_comShape
->
addItem
(
"卡车图案"
);
// 添加布局
QGridLayout
*
layout
=
new
QGridLayout
(
this
);
layout
->
addWidget
(
m_originalRenderArea
,
0
,
0
);
layout
->
addWidget
(
m_comShape
,
1
,
0
);
for
(
int
i
=
0
;
i
<
NumTransformendAreas
;
i
++
)
{
m_transformedRenderAreas
[
i
]
=
new
RenderArea
(
this
);
m_comOperation
[
i
]
=
new
QComboBox
(
this
);
m_comOperation
[
i
]
->
addItem
(
"默认状态"
);
m_comOperation
[
i
]
->
addItem
(
"旋转60度"
);
m_comOperation
[
i
]
->
addItem
(
"缩小75%"
);
m_comOperation
[
i
]
->
addItem
(
"坐标原点偏移(50,50)"
);
// 添加布局
layout
->
addWidget
(
m_transformedRenderAreas
[
i
],
0
,
i
+
1
);
layout
->
addWidget
(
m_comOperation
[
i
],
1
,
i
+
1
);
}
}
PaintingDemo/Transformations/widget.h
浏览文件 @
8b1b11ff
...
...
@@ -5,6 +5,7 @@
QT_BEGIN_NAMESPACE
class
QComboBox
;
class
RenderArea
;
QT_END_NAMESPACE
class
Widget
:
public
QWidget
...
...
@@ -24,5 +25,7 @@ private:
QComboBox
*
m_comShape
=
nullptr
;
// 绘制图案选择
QComboBox
*
m_comOperation
[
NumTransformendAreas
];
// 包含多个QComboBox指针的指针数组
QList
<
QPainterPath
>
m_shapes
;
// 绘制图案
RenderArea
*
m_originalRenderArea
=
nullptr
;
// 原始渲染区域
RenderArea
*
m_transformedRenderAreas
[
NumTransformendAreas
];
// 可进行缩放、偏移、旋转的区域
};
#endif // WIDGET_H
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录