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

相关推荐
Sylvia-girl5 小时前
Java——抽象类
java·开发语言
Yana.nice7 小时前
Bash函数详解
开发语言·chrome·bash
tomorrow.hello9 小时前
Java并发测试工具
java·开发语言·测试工具
晓13139 小时前
JavaScript加强篇——第四章 日期对象与DOM节点(基础)
开发语言·前端·javascript
老胖闲聊9 小时前
Python I/O 库【输入输出】全面详解
开发语言·python
她说人狗殊途10 小时前
java.net.InetAddress
java·开发语言
天使day10 小时前
Cursor的使用
java·开发语言·ai
Dxy123931021611 小时前
Python ExcelWriter详解:从基础到高级的完整指南
开发语言·python
源代码•宸13 小时前
C++高频知识点(十三)
开发语言·c++·经验分享·面经
wa的一声哭了13 小时前
python基础知识pip配置pip.conf文件
java·服务器·开发语言·python·pip·risc-v·os