Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
mahuifa
QMDemo
提交
edef4c88
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,发现更多精彩内容 >>
提交
edef4c88
编写于
7月 19, 2022
作者:
mahuifa
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'Dev'
上级
ce45273d
84458338
变更
13
隐藏空白更改
内联
并排
Showing
13 changed file
with
953 addition
and
0 deletion
+953
-0
PaintingDemo/PaintingDemo.assets/basicdrawing.gif
PaintingDemo/PaintingDemo.assets/basicdrawing.gif
+0
-0
PaintingDemo/PaintingDemo.md
PaintingDemo/PaintingDemo.md
+30
-0
PaintingDemo/PaintingDemo.pro
PaintingDemo/PaintingDemo.pro
+3
-0
PaintingDemo/basicdrawing/1.png
PaintingDemo/basicdrawing/1.png
+0
-0
PaintingDemo/basicdrawing/basicdrawing.pro
PaintingDemo/basicdrawing/basicdrawing.pro
+59
-0
PaintingDemo/basicdrawing/image.qrc
PaintingDemo/basicdrawing/image.qrc
+5
-0
PaintingDemo/basicdrawing/main.cpp
PaintingDemo/basicdrawing/main.cpp
+11
-0
PaintingDemo/basicdrawing/renderarea.cpp
PaintingDemo/basicdrawing/renderarea.cpp
+236
-0
PaintingDemo/basicdrawing/renderarea.h
PaintingDemo/basicdrawing/renderarea.h
+72
-0
PaintingDemo/basicdrawing/widget.cpp
PaintingDemo/basicdrawing/widget.cpp
+217
-0
PaintingDemo/basicdrawing/widget.h
PaintingDemo/basicdrawing/widget.h
+47
-0
PaintingDemo/basicdrawing/widget.ui
PaintingDemo/basicdrawing/widget.ui
+272
-0
README.md
README.md
+1
-0
未找到文件。
PaintingDemo/PaintingDemo.assets/basicdrawing.gif
0 → 100644
浏览文件 @
edef4c88
261.9 KB
PaintingDemo/PaintingDemo.md
0 → 100644
浏览文件 @
edef4c88
# Qt绘图Demo
[toc]
---
## 1、说明
| 类名 | 功能 |
| ------------ | -------------------- |
| basicdrawing | QPainter基本绘图Demo |
| | |
## 2、相关博客
## 3、实现效果
### 1.1 basicdrawing
> 1. 绘制原点和坐标轴;
> 2. 绘制线段、散点、多线段、多边形、矩形、圆角矩形、椭圆、弧线、弦形、扇形、绘制路径、文字、图片;
> 3. 可设置画笔样式、笔帽样式、线条连接样式、笔刷样式、画笔粗细、颜色、抗锯齿;
> 4. 可设置坐标原点水平、垂直偏移、旋转、缩放。
![
basicdrawing
](
PaintingDemo.assets/basicdrawing.gif
)
\ No newline at end of file
PaintingDemo/PaintingDemo.pro
0 → 100644
浏览文件 @
edef4c88
TEMPLATE
=
subdirs
SUBDIRS
+=
basicdrawing
#
QPainter
基本绘图
Demo
PaintingDemo/basicdrawing/1.png
0 → 100644
浏览文件 @
edef4c88
4.0 KB
PaintingDemo/basicdrawing/basicdrawing.pro
0 → 100644
浏览文件 @
edef4c88
#---------------------------------------------------------------------------------------
#
@
功能:
QPainter
基本绘图
Demo
#
@
编译器:
MSVC2017
-
64
(也支持其它编译器)
#
@
Qt
IDE
:
D
:/
Qt
/
Qt5
.
12.5
/
Tools
/
QtCreator
/
share
/
qtcreator
#
#
@
开发者
mhf
#
@
邮箱
1603291350
@
qq
.
com
#
@
时间
2022
-
07
-
18
20
:
23
:
48
#
@
备注
#---------------------------------------------------------------------------------------
QT
+=
core
gui
greaterThan
(
QT_MAJOR_VERSION
,
4
)
:
QT
+=
widgets
CONFIG
+=
c
++
11
#
The
following
define
makes
your
compiler
emit
warnings
if
you
use
#
any
Qt
feature
that
has
been
marked
deprecated
(
the
exact
warnings
#
depend
on
your
compiler
).
Please
consult
the
documentation
of
the
#
deprecated
API
in
order
to
know
how
to
port
your
code
away
from
it
.
DEFINES
+=
QT_DEPRECATED_WARNINGS
#
You
can
also
make
your
code
fail
to
compile
if
it
uses
deprecated
APIs
.
#
In
order
to
do
so
,
uncomment
the
following
line
.
#
You
can
also
select
to
disable
deprecated
APIs
only
up
to
a
certain
version
of
Qt
.
#
DEFINES
+=
QT_DISABLE_DEPRECATED_BEFORE
=
0
x060000
#
disables
all
the
APIs
deprecated
before
Qt
6.0
.
0
SOURCES
+=
\
main
.
cpp
\
renderarea
.
cpp
\
widget
.
cpp
HEADERS
+=
\
renderarea
.
h
\
widget
.
h
FORMS
+=
\
widget
.
ui
#
Default
rules
for
deployment
.
qnx
:
target
.
path
=
/
tmp
/
$$
{
TARGET
}
/
bin
else
:
unix
:
!
android
:
target
.
path
=
/
opt
/
$$
{
TARGET
}
/
bin
!
isEmpty
(
target
.
path
)
:
INSTALLS
+=
target
contains
(
QT_ARCH
,
i386
){
#
使用
32
位编译器
DESTDIR
=
$$
PWD
/../
bin
#
程序输出路径
}
else
{
DESTDIR
=
$$
PWD
/../
bin64
#
使用
64
位编译器
}
#
msvc
编译器使用
utf
-
8
编码(好像只在
msvc2017以后才有效
)
msvc
{
QMAKE_CFLAGS
+=
/
utf
-
8
QMAKE_CXXFLAGS
+=
/
utf
-
8
}
RESOURCES
+=
\
image
.
qrc
PaintingDemo/basicdrawing/image.qrc
0 → 100644
浏览文件 @
edef4c88
<RCC>
<qresource prefix="/">
<file>1.png</file>
</qresource>
</RCC>
PaintingDemo/basicdrawing/main.cpp
0 → 100644
浏览文件 @
edef4c88
#include "widget.h"
#include <QApplication>
int
main
(
int
argc
,
char
*
argv
[])
{
QApplication
a
(
argc
,
argv
);
Widget
w
;
w
.
show
();
return
a
.
exec
();
}
PaintingDemo/basicdrawing/renderarea.cpp
0 → 100644
浏览文件 @
edef4c88
#include "renderarea.h"
#include <QDebug>
#include <QFile>
#include <QPainter>
#include <QTextDocument>
#include <QWheelEvent>
RenderArea
::
RenderArea
(
QWidget
*
parent
)
:
QWidget
(
parent
)
{
// 设置背景填充色
this
->
setBackgroundRole
(
QPalette
::
Base
);
this
->
setAutoFillBackground
(
true
);
m_pixmap
.
load
(
"://1.png"
);
m_pen
.
setWidth
(
10
);
// 画笔默认宽度为10,便于观察效果
}
/**
* @brief 设置坐标原点偏移值
* @param dx
* @param dy
*/
void
RenderArea
::
setTranslate
(
qreal
dx
,
qreal
dy
)
{
m_offset
.
setX
(
dx
);
m_offset
.
setY
(
dy
);
this
->
update
();
}
/**
* @brief 设置渲染是否抗锯齿
* @param transformed
*/
void
RenderArea
::
setAntialiased
(
bool
transformed
)
{
m_antialiased
=
transformed
;
this
->
update
();
}
/**
* @brief 设置画笔
* @param pen
*/
void
RenderArea
::
setPen
(
const
QPen
&
pen
)
{
m_pen
=
pen
;
this
->
update
();
}
/**
* @brief 设置笔刷
* @param brush
*/
void
RenderArea
::
setBrush
(
const
QBrush
&
brush
)
{
m_brush
=
brush
;
this
->
update
();
}
/**
* @brief 设置旋转角度
* @param rotate
*/
void
RenderArea
::
setRotate
(
qreal
rotate
)
{
m_rotate
=
rotate
;
this
->
update
();
}
/**
* @brief 绘制主要图案
* @param shape
*/
void
RenderArea
::
setShape
(
RenderArea
::
Shape
shape
)
{
m_shape
=
shape
;
this
->
update
();
}
void
RenderArea
::
paintEvent
(
QPaintEvent
*
event
)
{
QWidget
::
paintEvent
(
event
);
QPainter
painter
(
this
);
if
(
m_antialiased
)
// 设置绘制渲染抗锯齿
{
painter
.
setRenderHint
(
QPainter
::
Antialiasing
);
}
painter
.
translate
(
m_offset
);
// 偏移坐标原点
painter
.
rotate
(
m_rotate
);
// 旋转
drawOrigin
(
painter
);
// 原点画笔恒定不变
painter
.
scale
(
m_zoom
,
m_zoom
);
painter
.
setPen
(
m_pen
);
painter
.
setBrush
(
m_brush
);
drawPattern
(
painter
);
}
/**
* @brief 画原点
* @param painter
*/
void
RenderArea
::
drawOrigin
(
QPainter
&
painter
)
{
painter
.
save
();
// 保存画家状态
painter
.
setPen
(
QPen
(
QColor
(
0
,
0
,
0
),
2
));
painter
.
drawText
(
QRect
(
10
,
-
30
,
100
,
25
),
Qt
::
AlignLeft
,
QString
(
"当前角度:%1"
).
arg
(
m_rotate
));
// 画原点
painter
.
drawPoint
(
0
,
0
);
painter
.
drawEllipse
(
-
5
,
-
5
,
10
,
10
);
painter
.
drawLine
(
QPoint
(
0
,
0
),
QPoint
(
100
,
0
));
// 画X轴
painter
.
drawLine
(
QPoint
(
0
,
0
),
QPoint
(
0
,
100
));
// 画Y轴
QPainterPath
path
;
path
.
moveTo
(
100
,
-
3
);
path
.
lineTo
(
106
,
0
);
path
.
lineTo
(
100
,
3
);
painter
.
drawPath
(
path
);
// 画X轴箭头
painter
.
rotate
(
90
);
painter
.
drawPath
(
path
);
// 画Y轴箭头
painter
.
restore
();
// 恢复画家状态
}
/**
* @brief 画主要图案
* @param painter
*/
void
RenderArea
::
drawPattern
(
QPainter
&
painter
)
{
painter
.
save
();
QRect
rect
(
10
,
20
,
280
,
260
);
static
const
QPoint
points
[
4
]
=
{
QPoint
(
100
,
280
),
QPoint
(
120
,
30
),
QPoint
(
280
,
60
),
QPoint
(
290
,
270
)
};
int
startAngle
=
20
*
16
;
// 开始角度 startAngle和arcLength必须以1/16度指定
int
arcLength
=
120
*
16
;
// 弧线角度
// 创建一个绘制路径
QPainterPath
path
;
path
.
moveTo
(
20
,
180
);
// 移动开始点
path
.
lineTo
(
20
,
30
);
// 添加一条(80到30d)竖线
path
.
cubicTo
(
QPoint
(
180
,
0
),
QPoint
(
80
,
80
),
QPoint
(
180
,
180
));
// 添加一条曲线
switch
(
m_shape
)
{
case
Line
:
// 绘制线段
{
painter
.
drawLine
(
rect
.
bottomLeft
(),
rect
.
topRight
());
break
;
}
case
Points
:
// 绘制4个点
{
painter
.
drawPoints
(
points
,
4
);
break
;
}
case
Polyline
:
// 画多线段
{
painter
.
drawPolyline
(
points
,
4
);
break
;
}
case
Polygon
:
// 画多边形
{
painter
.
drawPolygon
(
points
,
4
);
break
;
}
case
Rect
:
// 画矩形
{
painter
.
drawRect
(
rect
);
break
;
}
case
RoundedRect
:
// 画圆角矩形
{
painter
.
drawRoundedRect
(
rect
,
25
,
25
,
Qt
::
AbsoluteSize
);
// 使用绝对值(像素)
// painter.drawRoundedRect(rect, 25, 25, Qt::RelativeSize); // 指定相对于边界矩形的大小,通常使用百分比测量值。
break
;
}
case
Ellipse
:
// 画椭圆
{
painter
.
drawEllipse
(
rect
);
break
;
}
case
Arc
:
// 从20度开始,画一个120的弧线(角度的正值表示逆时针方向,负值表示顺时针方向。零度位于3点钟位置)
{
painter
.
drawArc
(
rect
,
startAngle
,
arcLength
);
break
;
}
case
Chord
:
// 绘制由给定矩形、startAngle和arcLength定义的弦
{
painter
.
drawChord
(
rect
,
startAngle
,
arcLength
);
break
;
}
case
Pie
:
// 绘制饼图(扇形)
{
painter
.
drawPie
(
rect
,
startAngle
,
arcLength
);
break
;
}
case
Path
:
// 绘制由QPainterPath组成的图形
{
painter
.
drawPath
(
path
);
break
;
}
case
Text
:
// 绘制文本
{
painter
.
drawText
(
rect
,
Qt
::
AlignCenter
,
"Qt Text
\n
绘制文本"
);
break
;
}
case
Pixmap
:
// 绘制图片
{
painter
.
drawPixmap
(
10
,
10
,
m_pixmap
);
break
;
}
}
painter
.
restore
();
}
/**
* @brief 鼠标滚轮缩放事件
* @param event
*/
void
RenderArea
::
wheelEvent
(
QWheelEvent
*
event
)
{
if
(
event
->
angleDelta
().
y
()
>
0
)
// 放大
{
m_zoom
+=
0.2
;
}
else
// 缩小
{
m_zoom
-=
0.2
;
}
this
->
update
();
QWidget
::
wheelEvent
(
event
);
}
PaintingDemo/basicdrawing/renderarea.h
0 → 100644
浏览文件 @
edef4c88
/******************************************************************************
* @文件名 renderarea.h
* @功能 1、绘制原点和坐标轴;
* 2、绘制线段、散点、多线段、多边形、矩形、圆角矩形、椭圆、弧线、弦形、扇形、绘制路径、文字、图片;
* 3、可设置画笔样式、笔帽样式、线条连接样式、笔刷样式、画笔粗细、颜色、抗锯齿;
* 4、可设置坐标原点水平、垂直偏移、旋转、缩放。
*
* @开发者 mhf
* @邮箱 1603291350@qq.com
* @时间 2022/07/18
* @备注
*****************************************************************************/
#ifndef RENDERAREA_H
#define RENDERAREA_H
#include <QWidget>
#include <QStyle>
#include <QPen>
class
RenderArea
:
public
QWidget
{
Q_OBJECT
public:
// 绘制形状
enum
Shape
{
Line
,
Points
,
Polyline
,
Polygon
,
Rect
,
RoundedRect
,
Ellipse
,
Arc
,
Chord
,
Pie
,
Path
,
Text
,
Pixmap
};
Q_ENUM
(
Shape
)
// 该宏向元对象系统注册一个枚举类型,便于通过QMetaEnum获取枚举信息
public:
explicit
RenderArea
(
QWidget
*
parent
=
nullptr
);
signals:
public
slots
:
void
setTranslate
(
qreal
dx
,
qreal
dy
);
void
setAntialiased
(
bool
antialiased
);
void
setPen
(
const
QPen
&
pen
);
void
setBrush
(
const
QBrush
&
brush
);
void
setRotate
(
qreal
rotate
);
void
setShape
(
Shape
shape
);
private:
void
drawOrigin
(
QPainter
&
painter
);
void
drawPattern
(
QPainter
&
painter
);
protected:
void
paintEvent
(
QPaintEvent
*
event
)
override
;
void
wheelEvent
(
QWheelEvent
*
event
)
override
;
private:
QPointF
m_offset
;
// 坐标原点
QPen
m_pen
;
// 画笔
QBrush
m_brush
;
// 笔刷
qreal
m_rotate
=
0
;
// 旋转角度
Shape
m_shape
=
Line
;
// 绘制图案
qreal
m_zoom
=
1
;
// 缩放级别
QPixmap
m_pixmap
;
// 绘制的图片
bool
m_antialiased
=
false
;
// 渲染是否抗锯齿
};
#endif // RENDERAREA_H
PaintingDemo/basicdrawing/widget.cpp
0 → 100644
浏览文件 @
edef4c88
#include "widget.h"
#include "ui_widget.h"
#include "renderarea.h"
#include <QColorDialog>
#include <QDebug>
#include <QMetaEnum>
Widget
::
Widget
(
QWidget
*
parent
)
:
QWidget
(
parent
)
,
ui
(
new
Ui
::
Widget
)
{
ui
->
setupUi
(
this
);
this
->
setWindowTitle
(
"QPainter基本绘图Demo"
);
init
();
connectSlots
();
}
Widget
::~
Widget
()
{
delete
ui
;
}
void
Widget
::
init
()
{
// 获取画图形状Shape枚举名称和值,写入下拉框
QMetaEnum
metaEnum
=
QMetaEnum
::
fromType
<
RenderArea
::
Shape
>
();
for
(
int
i
=
0
;
i
<
metaEnum
.
keyCount
();
++
i
)
{
ui
->
com_shape
->
addItem
(
metaEnum
.
key
(
i
),
metaEnum
.
value
(
i
));
}
// 画笔样式枚举
metaEnum
=
QMetaEnum
::
fromType
<
Qt
::
PenStyle
>
();
for
(
int
i
=
0
;
i
<
metaEnum
.
keyCount
();
++
i
)
{
ui
->
com_penStyle
->
addItem
(
metaEnum
.
key
(
i
),
metaEnum
.
value
(
i
));
}
// 画笔笔帽样式枚举
metaEnum
=
QMetaEnum
::
fromType
<
Qt
::
PenCapStyle
>
();
for
(
int
i
=
0
;
i
<
metaEnum
.
keyCount
();
++
i
)
{
ui
->
com_penCapStyle
->
addItem
(
metaEnum
.
key
(
i
),
metaEnum
.
value
(
i
));
}
// 线段转角处连接样式枚举
metaEnum
=
QMetaEnum
::
fromType
<
Qt
::
PenJoinStyle
>
();
for
(
int
i
=
0
;
i
<
metaEnum
.
keyCount
();
++
i
)
{
ui
->
com_penJoinStyle
->
addItem
(
metaEnum
.
key
(
i
),
metaEnum
.
value
(
i
));
}
// 笔刷样式枚举
metaEnum
=
QMetaEnum
::
fromType
<
Qt
::
BrushStyle
>
();
for
(
int
i
=
0
;
i
<
metaEnum
.
keyCount
();
++
i
)
{
ui
->
com_brushStyle
->
addItem
(
metaEnum
.
key
(
i
),
metaEnum
.
value
(
i
));
}
ui
->
widget
->
installEventFilter
(
this
);
ui
->
com_shape
->
setCurrentIndex
(
0
);
ui
->
com_penStyle
->
setCurrentIndex
(
1
);
}
void
Widget
::
connectSlots
()
{
connect
(
ui
->
com_penStyle
,
QOverload
<
int
>::
of
(
&
QComboBox
::
activated
),
this
,
&
Widget
::
setPen
);
connect
(
ui
->
com_penCapStyle
,
QOverload
<
int
>::
of
(
&
QComboBox
::
activated
),
this
,
&
Widget
::
setPen
);
connect
(
ui
->
com_penJoinStyle
,
QOverload
<
int
>::
of
(
&
QComboBox
::
activated
),
this
,
&
Widget
::
setPen
);
connect
(
ui
->
spinBox
,
QOverload
<
int
>::
of
(
&
QSpinBox
::
valueChanged
),
this
,
&
Widget
::
setPen
);
}
/**
* @brief 设置画笔
*/
void
Widget
::
setPen
()
{
QPen
pen
;
pen
.
setStyle
(
Qt
::
PenStyle
(
ui
->
com_penStyle
->
currentData
().
toInt
()));
// 设置画笔样式
Qt
::
PenJoinStyle
joinStyle
=
Qt
::
PenJoinStyle
(
ui
->
com_penJoinStyle
->
currentData
().
toInt
());
pen
.
setJoinStyle
(
joinStyle
);
if
(
Qt
::
MiterJoin
==
joinStyle
)
{
pen
.
setMiterLimit
(
20
);
}
pen
.
setWidth
(
ui
->
spinBox
->
value
());
// 设置画笔线宽
pen
.
setColor
(
m_color
);
// 设置画笔颜色
// pen.setBrush()
pen
.
setCapStyle
(
Qt
::
PenCapStyle
(
ui
->
com_penCapStyle
->
currentData
().
toInt
()));
// 设置画笔笔帽样式
ui
->
widget
->
setPen
(
pen
);
}
/**
* @brief 事件过滤器
* @param watched
* @param event
* @return
*/
bool
Widget
::
eventFilter
(
QObject
*
watched
,
QEvent
*
event
)
{
if
(
watched
==
ui
->
widget
&&
event
->
type
()
==
QEvent
::
Resize
)
{
ui
->
horizontalSlider_origin
->
setMaximum
(
ui
->
widget
->
width
());
ui
->
verticalSlider_origin
->
setMaximum
(
ui
->
widget
->
height
());
ui
->
horizontalSlider_origin
->
setValue
(
0
);
ui
->
verticalSlider_origin
->
setValue
(
ui
->
widget
->
height
());
}
return
QWidget
::
eventFilter
(
watched
,
event
);
}
/**
* @brief 水平偏移原点
* @param value
*/
void
Widget
::
on_horizontalSlider_origin_valueChanged
(
int
value
)
{
ui
->
widget
->
setTranslate
(
value
,
ui
->
widget
->
height
()
-
ui
->
verticalSlider_origin
->
value
());
}
/**
* @brief 垂直偏移原点
* @param value
*/
void
Widget
::
on_verticalSlider_origin_valueChanged
(
int
value
)
{
ui
->
widget
->
setTranslate
(
ui
->
horizontalSlider_origin
->
value
(),
ui
->
widget
->
height
()
-
value
);
}
/**
* @brief 设置抗锯齿
* @param checked
*/
void
Widget
::
on_checkBox_clicked
(
bool
checked
)
{
ui
->
widget
->
setAntialiased
(
checked
);
}
/**
* @brief 设置画笔颜色
*/
void
Widget
::
on_but_color_clicked
()
{
m_color
=
QColorDialog
::
getColor
(
m_color
,
this
);
setPen
();
}
/**
* @brief 设置笔刷
* @param index
*/
void
Widget
::
on_com_brushStyle_activated
(
int
index
)
{
Q_UNUSED
(
index
)
Qt
::
BrushStyle
style
=
Qt
::
BrushStyle
(
ui
->
com_brushStyle
->
currentData
().
toInt
());
switch
(
style
)
{
case
Qt
::
LinearGradientPattern
:
// 添加线性渐变图案
{
QLinearGradient
linearGradient
(
0
,
0
,
100
,
100
);
linearGradient
.
setColorAt
(
0.0
,
Qt
::
white
);
linearGradient
.
setColorAt
(
0.2
,
Qt
::
green
);
linearGradient
.
setColorAt
(
1.0
,
Qt
::
black
);
ui
->
widget
->
setBrush
(
linearGradient
);
break
;
}
case
Qt
::
RadialGradientPattern
:
// 添加径向渐变图案
{
QRadialGradient
radialGradient
(
50
,
50
,
50
,
70
,
70
);
radialGradient
.
setColorAt
(
0.0
,
Qt
::
white
);
radialGradient
.
setColorAt
(
0.2
,
Qt
::
green
);
radialGradient
.
setColorAt
(
1.0
,
Qt
::
black
);
ui
->
widget
->
setBrush
(
radialGradient
);
break
;
}
case
Qt
::
ConicalGradientPattern
:
// 添加锥形渐变图案
{
QConicalGradient
conicalGradient
(
50
,
50
,
150
);
conicalGradient
.
setColorAt
(
0.0
,
Qt
::
white
);
conicalGradient
.
setColorAt
(
0.2
,
Qt
::
green
);
conicalGradient
.
setColorAt
(
1.0
,
Qt
::
black
);
ui
->
widget
->
setBrush
(
conicalGradient
);
break
;
}
case
Qt
::
TexturePattern
:
// 添加图片纹理笔刷
{
ui
->
widget
->
setBrush
(
QBrush
(
QPixmap
(
"C:/Users/mhf/Pictures/grid.PNG"
)));
break
;
}
default:
{
QBrush
brush
;
brush
.
setStyle
(
style
);
brush
.
setColor
(
Qt
::
red
);
ui
->
widget
->
setBrush
(
brush
);
break
;
}
}
}
/**
* @brief 设置绘制图案类型
* @param index
*/
void
Widget
::
on_com_shape_activated
(
int
index
)
{
Q_UNUSED
(
index
)
ui
->
widget
->
setShape
(
RenderArea
::
Shape
(
ui
->
com_shape
->
currentData
().
toInt
()));
}
/**
* @brief 旋转
* @param value
*/
void
Widget
::
on_dial_rotate_valueChanged
(
int
value
)
{
ui
->
widget
->
setRotate
((
value
+
90
)
%
360
);
// 由于QDial默认0为竖直向下方向,QPainter默认0度为向右,所以需要+90度
}
PaintingDemo/basicdrawing/widget.h
0 → 100644
浏览文件 @
edef4c88
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
QT_BEGIN_NAMESPACE
namespace
Ui
{
class
Widget
;
}
QT_END_NAMESPACE
class
Widget
:
public
QWidget
{
Q_OBJECT
public:
Widget
(
QWidget
*
parent
=
nullptr
);
~
Widget
();
private:
void
init
();
void
connectSlots
();
protected:
bool
eventFilter
(
QObject
*
watched
,
QEvent
*
event
)
override
;
private
slots
:
void
setPen
();
void
on_horizontalSlider_origin_valueChanged
(
int
value
);
void
on_verticalSlider_origin_valueChanged
(
int
value
);
void
on_checkBox_clicked
(
bool
checked
);
void
on_but_color_clicked
();
void
on_com_brushStyle_activated
(
int
index
);
void
on_com_shape_activated
(
int
index
);
void
on_dial_rotate_valueChanged
(
int
value
);
private:
Ui
::
Widget
*
ui
;
QColor
m_color
;
QBrush
m_penBrush
;
};
#endif // WIDGET_H
PaintingDemo/basicdrawing/widget.ui
0 → 100644
浏览文件 @
edef4c88
<?xml version="1.0" encoding="UTF-8"?>
<ui
version=
"4.0"
>
<class>
Widget
</class>
<widget
class=
"QWidget"
name=
"Widget"
>
<property
name=
"geometry"
>
<rect>
<x>
0
</x>
<y>
0
</y>
<width>
946
</width>
<height>
699
</height>
</rect>
</property>
<property
name=
"windowTitle"
>
<string>
Widget
</string>
</property>
<layout
class=
"QGridLayout"
name=
"gridLayout"
>
<item
row=
"0"
column=
"2"
rowspan=
"2"
>
<widget
class=
"QDockWidget"
name=
"dockWidget"
>
<property
name=
"sizePolicy"
>
<sizepolicy
hsizetype=
"Fixed"
vsizetype=
"Preferred"
>
<horstretch>
0
</horstretch>
<verstretch>
0
</verstretch>
</sizepolicy>
</property>
<property
name=
"minimumSize"
>
<size>
<width>
152
</width>
<height>
531
</height>
</size>
</property>
<property
name=
"maximumSize"
>
<size>
<width>
270
</width>
<height>
524287
</height>
</size>
</property>
<widget
class=
"QWidget"
name=
"dockWidgetContents"
>
<layout
class=
"QGridLayout"
name=
"gridLayout_3"
>
<property
name=
"leftMargin"
>
<number>
0
</number>
</property>
<property
name=
"rightMargin"
>
<number>
0
</number>
</property>
<item
row=
"0"
column=
"0"
colspan=
"2"
>
<layout
class=
"QGridLayout"
name=
"gridLayout_2"
>
<item
row=
"1"
column=
"1"
>
<widget
class=
"QLabel"
name=
"label"
>
<property
name=
"text"
>
<string>
形状:
</string>
</property>
<property
name=
"alignment"
>
<set>
Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter
</set>
</property>
</widget>
</item>
<item
row=
"5"
column=
"1"
>
<widget
class=
"QLabel"
name=
"label_4"
>
<property
name=
"text"
>
<string>
笔帽样式:
</string>
</property>
<property
name=
"alignment"
>
<set>
Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter
</set>
</property>
</widget>
</item>
<item
row=
"3"
column=
"1"
>
<widget
class=
"QLabel"
name=
"label_2"
>
<property
name=
"text"
>
<string>
画笔样式:
</string>
</property>
<property
name=
"alignment"
>
<set>
Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter
</set>
</property>
</widget>
</item>
<item
row=
"2"
column=
"1"
>
<widget
class=
"QComboBox"
name=
"com_shape"
>
<property
name=
"currentIndex"
>
<number>
-1
</number>
</property>
</widget>
</item>
<item
row=
"4"
column=
"1"
>
<widget
class=
"QComboBox"
name=
"com_penStyle"
/>
</item>
<item
row=
"14"
column=
"1"
>
<widget
class=
"QCheckBox"
name=
"checkBox"
>
<property
name=
"text"
>
<string>
抗锯齿
</string>
</property>
</widget>
</item>
<item
row=
"9"
column=
"1"
>
<widget
class=
"QLabel"
name=
"label_6"
>
<property
name=
"text"
>
<string>
笔刷样式:
</string>
</property>
<property
name=
"alignment"
>
<set>
Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter
</set>
</property>
</widget>
</item>
<item
row=
"10"
column=
"1"
>
<widget
class=
"QComboBox"
name=
"com_brushStyle"
>
<property
name=
"toolTip"
>
<string>
在不封闭的图形中无效,如直线、弧形等
</string>
</property>
</widget>
</item>
<item
row=
"8"
column=
"1"
>
<widget
class=
"QComboBox"
name=
"com_penJoinStyle"
>
<property
name=
"toolTip"
>
<string>
线条连接样式(在单条直线或有弧形的图形无效,如圆角矩形、椭圆)
</string>
</property>
</widget>
</item>
<item
row=
"12"
column=
"1"
>
<widget
class=
"QSpinBox"
name=
"spinBox"
>
<property
name=
"singleStep"
>
<number>
1
</number>
</property>
<property
name=
"value"
>
<number>
10
</number>
</property>
</widget>
</item>
<item
row=
"6"
column=
"1"
>
<widget
class=
"QComboBox"
name=
"com_penCapStyle"
>
<property
name=
"toolTip"
>
<string>
线条收头样式(绘制封闭形状时无效,如矩形、圆形等)
</string>
</property>
</widget>
</item>
<item
row=
"0"
column=
"1"
>
<widget
class=
"QPushButton"
name=
"but_color"
>
<property
name=
"text"
>
<string>
画笔颜色
</string>
</property>
</widget>
</item>
<item
row=
"7"
column=
"1"
>
<widget
class=
"QLabel"
name=
"label_5"
>
<property
name=
"text"
>
<string>
线条连接样式:
</string>
</property>
<property
name=
"alignment"
>
<set>
Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter
</set>
</property>
</widget>
</item>
<item
row=
"13"
column=
"1"
>
<widget
class=
"QLabel"
name=
"label_10"
>
<property
name=
"text"
>
<string>
旋转:
</string>
</property>
<property
name=
"alignment"
>
<set>
Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter
</set>
</property>
</widget>
</item>
<item
row=
"11"
column=
"1"
>
<widget
class=
"QLabel"
name=
"label_3"
>
<property
name=
"text"
>
<string>
画笔粗细:
</string>
</property>
<property
name=
"alignment"
>
<set>
Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter
</set>
</property>
</widget>
</item>
<item
row=
"15"
column=
"1"
>
<widget
class=
"QDial"
name=
"dial_rotate"
>
<property
name=
"minimumSize"
>
<size>
<width>
150
</width>
<height>
150
</height>
</size>
</property>
<property
name=
"maximum"
>
<number>
360
</number>
</property>
<property
name=
"value"
>
<number>
270
</number>
</property>
<property
name=
"sliderPosition"
>
<number>
270
</number>
</property>
<property
name=
"orientation"
>
<enum>
Qt::Horizontal
</enum>
</property>
<property
name=
"wrapping"
>
<bool>
true
</bool>
</property>
<property
name=
"notchTarget"
>
<double>
10.000000000000000
</double>
</property>
<property
name=
"notchesVisible"
>
<bool>
true
</bool>
</property>
</widget>
</item>
</layout>
</item>
<item
row=
"3"
column=
"1"
>
<spacer
name=
"verticalSpacer"
>
<property
name=
"orientation"
>
<enum>
Qt::Vertical
</enum>
</property>
<property
name=
"sizeHint"
stdset=
"0"
>
<size>
<width>
20
</width>
<height>
247
</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</widget>
</item>
<item
row=
"1"
column=
"1"
>
<widget
class=
"RenderArea"
name=
"widget"
native=
"true"
/>
</item>
<item
row=
"0"
column=
"1"
>
<widget
class=
"QSlider"
name=
"horizontalSlider_origin"
>
<property
name=
"toolTip"
>
<string>
原点水平偏移
</string>
</property>
<property
name=
"maximum"
>
<number>
700
</number>
</property>
<property
name=
"orientation"
>
<enum>
Qt::Horizontal
</enum>
</property>
<property
name=
"tickPosition"
>
<enum>
QSlider::TicksBelow
</enum>
</property>
</widget>
</item>
<item
row=
"1"
column=
"0"
>
<widget
class=
"QSlider"
name=
"verticalSlider_origin"
>
<property
name=
"toolTip"
>
<string>
原点垂直偏移
</string>
</property>
<property
name=
"maximum"
>
<number>
653
</number>
</property>
<property
name=
"value"
>
<number>
653
</number>
</property>
<property
name=
"orientation"
>
<enum>
Qt::Vertical
</enum>
</property>
<property
name=
"tickPosition"
>
<enum>
QSlider::TicksBelow
</enum>
</property>
</widget>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>
RenderArea
</class>
<extends>
QWidget
</extends>
<header>
renderarea.h
</header>
<container>
1
</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
README.md
浏览文件 @
edef4c88
...
...
@@ -22,6 +22,7 @@
| ProgramFramework |
[
用过的一些Qt程序模块设计架构Demo
](
./ProgramFramework/ProgramFramework.md
)
| |
| QSqlDemo |
[
QSql学习Demo
](
./QSqlDemo/QSql.md
)
| |
| QStyleDemo |
[
Qt样式美化设置Demo说明及演示
](
./QStyleDemo/QStyleDemo.md
)
| |
| PaintingDemo |
[
Qt绘图Demo
](
./PaintingDemo/PaintingDemo.md
)
| |
| | | |
| |
**使用到第三方库**
| |
| XlsxDemo |
[
Qt使用QXlsx操作Excel Demo
](
./XlsxDemo/XlsxDemo.md
)
| |
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录