Qt 绘画事件 QPainEvent ,QPainter QPen QBrush

#include "qtwidgetsapplication2.h"
#include 
#include 

QtWidgetsApplication2::QtWidgetsApplication2(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
	QTimer* timer = new QTimer(this);
	timer->start(1000);
	connect(timer, &QTimer::timeout, [=]() {
		m_x += 10;
		// 调用 paintEvent
		// repaint();  //旧版
		update();     // 新版
		
	});
	

}

// 重写绘画事件
void QtWidgetsApplication2::paintEvent(QPaintEvent * pEv)
{
	//实例化一个画家
	QPainter painter(this);

	// 画笔
	QPen pen(Qt::cyan);
	pen.setStyle(Qt::DotLine);
	pen.setWidth(2);
	painter.setPen(pen);

	// 笔刷
	QBrush brush(Qt::gray);
	brush.setStyle(Qt::Dense7Pattern);
	painter.setBrush(brush);

	// 线段
	painter.drawLine(QLine(0, 0, 50, 50));
	// 椭圆
	painter.drawEllipse(QPoint(100,50), 50, 50);
	// 长方形
	painter.drawRect(QRect(0, 50, 50, 100));
	// 文字
	painter.drawText(QRect(0, 150, 100, 50), " i am a worker.");

	// 移动坐标原点位置
	painter.translate(200, 0);
	// 记录painter状态
	painter.save();
	// 椭圆
	painter.drawEllipse(QPoint(100, 50), 50, 50);

	// 移动坐标原点位置
	painter.translate(200, 0);
	// 恢复painter上次记录的状态
	painter.restore();
	// 设置画图质量
	painter.setRenderHint(QPainter::Antialiasing);
	// 椭圆
	painter.drawEllipse(QPoint(100, 50), 50, 50);

	painter.translate(-200, 200);

	if (m_x > this->width())
	{
		m_x = 0;
	}
	// 画图片
	painter.drawPixmap(m_x, 0, QPixmap(":/sourse_picture/1234.jpg"));
}

你可能感兴趣的:(Qt,qt,开发语言,ui)