- 代码
#include "widget.h"
#include "ui_widget.h"
#include <QPainter>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
setPalette(QPalette(Qt::white));
setAutoFillBackground(true);
}
Widget::~Widget()
{
delete ui;
}
/**
* @brief Widget::paintEvent
* @param event
* @note 主要画了矩形,折线,图片,文字等,设置画刷、笔、字体等
*/
void Widget::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QPainter painter(this);
int w = this->width();
int h = this->height();
//抗锯齿不止这两样,其他请按F1
painter.setRenderHint(QPainter::Antialiasing);//图像抗锯齿
painter.setRenderHints(QPainter::TextAntialiasing);//文字抗锯齿
QPen pen;
pen.setColor(QColor(90,60,30,128));
pen.setStyle(Qt::DashDotDotLine);
pen.setWidth(5);
pen.setJoinStyle(Qt::BevelJoin);//边角处如何连接,
painter.setPen(pen);
QBrush brush;
brush.setColor(Qt::green);
brush.setStyle(Qt::CrossPattern);
painter.setBrush(brush);
QImage image(":images/main.jpg");
QFont font;
font.setPointSize(26);
font.setBold(true);
painter.setFont(font);
QRect rect(w/4,h/4,w/2,h/2);
QPoint points[] = {QPoint(w/5,h/5), QPoint(w/2,h*4/5), QPoint(w*4/5,h/5)};
painter.drawRect(rect);
painter.drawImage(rect,image);
painter.drawText(rect, Qt::AlignCenter,"Hello, Kitty!");
pen.setColor(Qt::lightGray);
painter.setPen(pen);
painter.drawPolyline(points, sizeof(points)/sizeof (points[0]));
}
-
界面
reference:
NOTICE
- 参考视频到此还没有结束,但是我忘记写笔记了,emmmm,大家想看的,可以去B站看