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

相关推荐
20242817李臻4 分钟前
20242817-李臻-课下作业:Qt和Sqlite
jvm·qt·sqlite
tmacfrank16 分钟前
Java 原生网络编程(BIO | NIO | Reactor 模式)
java·开发语言·网络
沐土Arvin18 分钟前
深入理解 requestIdleCallback:浏览器空闲时段的性能优化利器
开发语言·前端·javascript·设计模式·html
tyatyatya26 分钟前
MATLAB的神经网络工具箱
开发语言·神经网络·matlab
unityのkiven28 分钟前
C++中析构函数不设为virtual导致内存泄漏示例
开发语言·c++
keke101 小时前
Java【14_3】接口(Comparable和Comparator)、内部类-示例
java·开发语言·servlet
小破农1 小时前
C++篇——多态
开发语言·c++
Q_Q19632884751 小时前
python的漫画网站管理系统
开发语言·spring boot·python·django·flask·node.js·php
言之。1 小时前
Go 语言中接口类型转换为具体类型
开发语言·后端·golang
咖啡の猫1 小时前
JavaScript基础-创建对象的三种方式
开发语言·javascript·ecmascript