#include "sample_Qpainter.h"
#include"QPainter.h"
sample_Qpainter::sample_Qpainter(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
setPalette(QPalette(Qt::white));
setAutoFillBackground(true);
}
void sample_Qpainter::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.setRenderHint(QPainter::TextAntialiasing);
int W = this->width();//绘图区宽度
int H = this->height();//绘图高宽度
QRect rect(W / 4, H / 4, W / 2, H / 2);//中间区域矩形框
//设置笔画
QPen pen;
pen.setWidth(3);//线宽
pen.setColor(Qt::red);//划线颜色
pen.setStyle(Qt::SolidLine);//线的样式,实线,虚线等
pen.setJoinStyle(Qt::BevelJoin); //线点的连接点样式
painter.setPen(pen);
//设置画刷
QBrush brush;
brush.setColor(Qt::yellow);//画刷颜色
brush.setStyle(Qt::SolidPattern);//画刷填充样式
painter.setBrush(brush);
//绘图
painter.drawRect(rect);
}
//
/
#pragma once
#include <QtWidgets/QWidget>
#include "ui_sample_Qpainter.h"
class sample_Qpainter : public QWidget
{
Q_OBJECT
public:
sample_Qpainter(QWidget *parent = Q_NULLPTR);
private:
Ui::sample_QpainterClass ui;
protected:
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
};