qt之画图

QPainter

1、在头文件里重载

protected:

void paintEvent(QPaintEvent *event);

2、定义一个定时器,链接到槽函数 update

analylogTImer = new QTimer(this);

connect(analylogTImer,SIGNAL(timeout()),this, SLOT(update()));

analylogTImer->start(1000);

3、实现

void Widget::paintEvent(QPaintEvent *)

{

static const QPoint hourHand3 = {

QPoint(7, 8),

QPoint(68, 80),

QPoint(150, 40)

};

QColor hourColor(127,0,127);

int side = qMin(width(), height());
QPainter analylogPainter(this); //必须在这个函数里定义,否则图像画不出来

//

//analylogPainter->begin(this);

analylogPainter.setRenderHint(QPainter::Antialiasing);

analylogPainter.ranslate(width() / 2, height() / 2); //将坐标系统进行改变

//! 13 //! 14

analylogPainter.scale(side / 200.0, side / 200.0);

analylogPainter.setPen(Qt::NoPen);//设置无画笔

analylogPainter.setPen(hourColor);//设置画笔的颜色

analylogPainter.setBrush(hourColor);

analylogPainter.save();

analylogPainter.drawConvexPolygon(hourHand, 3);//根据点连接起来图像,中间用brush来填充

analylogPainter.rotate(90);//将画图的坐标旋转一定角度
analylogPainter.drawLine(10, 80, 96, 20);
analylogPainter.restore();//丢弃之前设置

}

问题,想知道这个PainerEvent什么时候才能被主动调用,而不用我设置一个定时器去调用他,还有画的的painter为什么必须要在PainerEvent里定义,此函数功能比较强大,要多看看帮助文件(光标在函数上面,然后按F1)

这个画图方法里学到了

a、QPainter可以用来画图,

首先定义一个QPainter,然后设定画笔颜色setpen,设定画刷setbrush,还可以用transtrate来选择你的画图坐标,方便与画图,

涉及到的类型有QColor QPoint, QLine QPainter

相关推荐
默 语6 小时前
HarmonyOS7 事件分发控制(二):onTouchIntercept 镂空玻璃卡——圆内自己接,圆外放行
android·java·开发语言
Amumu121387 小时前
TypeScript学习: 基础知识(二)
开发语言·typescript·ecmascript
sharemylove7 小时前
Java 异常与常用类 学习笔记
java·开发语言·经验分享·笔记·学习
小灰灰搞电子8 小时前
C 语言基础:动态内存管理详解
c语言·开发语言
打工仔折腾 AI8 小时前
L3 静态源码取证|工程评测:python-oracledb 驱动评估中的样本偏差陷阱
开发语言·人工智能·后端·python·性能优化
Amumu121388 小时前
TypeScript学习: 进阶知识
开发语言·学习·typescript
Helix25010 小时前
JDK异常处理最佳实践:受检异常、非受检异常、try-catch-finally与全局异常处理完整指南
java·开发语言·jdk·异常处理·全局异常·自定义异常·try-catch
时间的情敌11 小时前
Three.js 后处理辉光 Bloom、告警发光效果、管线流光、着色器流动管线(ShaderMaterial)
开发语言·javascript·three.js·着色器
Evand J11 小时前
【CIR系列2】UWB视距(LOS)与非视距(NLOS)信道冲激响应(CIR)建模与对比,MATLAB
开发语言·matlab·信号处理·nlos·cir
风落青芜11 小时前
C++入门:类和对象(二)
开发语言·数据结构·c++·算法