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

相关推荐
常利兵4 分钟前
Kotlin作用域函数全解:run/with/apply/let/also与this/it的魔法对决
android·开发语言·kotlin
幼稚园的山代王17 分钟前
Kotlin-基础语法练习一
android·开发语言·kotlin
重生成为编程大王24 分钟前
Java ConcurrentHashMap 深度解析
java·开发语言
tanyongxi661 小时前
C++ 特殊类设计与单例模式解析
java·开发语言·数据结构·c++·算法·单例模式
遗憾皆是温柔1 小时前
24. 什么是不可变对象,好处是什么
java·开发语言·面试·学习方法
wearegogog1231 小时前
C语言中的输入输出函数:构建程序交互的基石
c语言·开发语言·交互
Fine姐1 小时前
The Network Link Layer: 无线传感器中Delay Tolerant Networks – DTNs 延迟容忍网络
开发语言·网络·php·硬件架构
HAPPY酷2 小时前
给纯小白的Python操作 PDF 笔记
开发语言·python·pdf
liulilittle2 小时前
BFS寻路算法解析与实现
开发语言·c++·算法·宽度优先·寻路算法·寻路
阿珊和她的猫2 小时前
autofit.js: 自动调整HTML元素大小的JavaScript库
开发语言·javascript·html