首页 > 其他分享 >QT5笔记:18 QPainter基本绘图 完结撒花,感谢陪伴!!!

QT5笔记:18 QPainter基本绘图 完结撒花,感谢陪伴!!!

时间:2023-03-15 18:36:48浏览次数:57  
标签:Widget QT5 Qt 18 QPoint pen QPainter painter

image
image

  • 代码
#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]));
}
  • 界面

    画图、矩形和文字



Qt5 完结撒花,感谢陪伴!!!


reference:

​ 参考视频:https://www.bilibili.com/video/BV1AX4y1w7Nt

NOTICE

  1. 参考视频到此还没有结束,但是我忘记写笔记了,emmmm,大家想看的,可以去B站看

标签:Widget,QT5,Qt,18,QPoint,pen,QPainter,painter
From: https://www.cnblogs.com/echo-lovely/p/17219542.html

相关文章