#include "widget.h"
#include "ui_widget.h"
#include<QPainter>
#include<QBrush>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
}
void Widget::paintEvent(QPaintEvent *)
{
//实例化画家对象 this指绘图设备(在当前窗口画画)
QPainter painter(this);
//设置笔的颜色
QPen pen(QColor(255,0,0));
//设置笔的宽度
pen.setWidth(4);
//设置画笔风格
pen.setStyle(Qt::DashLine);
//拿起画笔
painter.setPen(pen);
//设置画刷
QBrush brush(Qt::blue);
//风格
brush.setStyle(Qt::Dense7Pattern);
//拿起画刷
painter.setBrush(brush);
//画一个直线
painter.drawLine(QPoint(0,0),QPoint(100,100));
painter.drawLine(QPoint(0,100),QPoint(100,0));
// 画圆
painter.drawEllipse(QPoint(100,100),50,50);
//画椭圆
painter.drawEllipse(QPoint(100,100),100,50);
//画矩形
painter.drawRect(QRect(75,75,50,50));
//写文字
painter.drawText(QRect(200,200,120,50),"好好学习,天天向上!");
}
Widget::~Widget()
{
delete ui;
}