// Widget.h
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
Q_OBJECT

public:
explicit Widget(QWidget *parent = 0);

// 绘图事件
void paintEvent(QPaintEvent *);

int posX = 0;

~Widget();

private:
Ui::Widget *ui;
};

#endif // WIDGET_H
// Widget.cpp

#include "Widget.h"
#include "ui_Widget.h"
#include <QPainter>

Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);

connect(this->ui->moveButton, QPushButton::clicked, [=](){
update();
});

}

void Widget::paintEvent(QPaintEvent *ev) {
/*
// 实例化画家对象, this指定的是绘画设备
QPainter painter(this);

// 让画家使用带颜色的笔
QPen pen(QColor(255, 0, 0));
pen.setWidth(3);
pen.setStyle(Qt::DotLine); // 画线风格
painter.setPen(pen);

// 设置画刷
QBrush brush(Qt::green);
// 画家使用画刷
brush.setStyle(Qt::Dense7Pattern); // 画刷风格
painter.setBrush(brush);


// 画线
painter.drawLine(QPoint(0, 0), QPoint(100, 100));

// 画椭圆
painter.drawEllipse(QPoint(100, 100), 100, 50);

// 画矩形
painter.drawRect(QRect(20, 20, 50, 50));

// 画文字
painter.drawText(QPoint(10, 200), "tqz");
*/


/*
// 高级设置
QPainter painter(this);
painter.drawEllipse(QPoint(100, 50), 50, 50);

// 设置抗锯齿能力, 效率较低
painter.setRenderHint(QPainter::Antialiasing);
painter.drawEllipse(QPoint(200, 50), 50, 50);

// 画矩形
painter.drawRect(QRect(20, 20, 50, 50));
painter.translate(100, 0); // 移动画家的坐标
painter.save(); // 保存画家状态

painter.drawRect(QRect(20, 20, 50, 50));

painter.translate(100, 0);

painter.restore(); // 还原画家保存状态
painter.drawRect(QRect(20, 20, 50, 50));
*/


/*
// 利用画家来画图片
QPainter painter(this);
this->posX += 2;
painter.drawPixmap(this->posX, 10, QPixmap(":/Image/xxy.png"));
*/
}

Widget::~Widget()
{
delete ui;
}