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 hourHand[3] = {

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

相关推荐
Bruce_Liuxiaowei13 分钟前
基于Python+Flask的MCP SDK响应式文档展示系统设计与实现
开发语言·python·flask·mcp
chuxinweihui20 分钟前
数据结构——栈与队列
c语言·开发语言·数据结构·学习·算法·链表
我不是程序猿儿20 分钟前
[C#]反射的实战应用,实际数据模拟
开发语言·c#
wt_cs32 分钟前
身份证实名认证接口数字时代的信任基石-node.js实名认证集成
开发语言·node.js·php
爱编程的鱼1 小时前
C# 结构(Struct)
开发语言·人工智能·算法·c#
只可远观1 小时前
Flutter Dart 循环语句 for while do..while break、continue
开发语言·javascript·ecmascript
吴_知遇2 小时前
【华为OD机试真题】428、连续字母长度 | 机试真题+思路参考+代码解析(E卷)(C++)
开发语言·c++·华为od
basketball6162 小时前
Python torchvision.transforms 下常用图像处理方法
开发语言·图像处理·python
宁酱醇3 小时前
各种各样的bug合集
开发语言·笔记·python·gitlab·bug
啊吧怪不啊吧3 小时前
Linux常见指令介绍下(入门级)
linux·开发语言·centos