【嵌入式——QT】QPainter基本绘图

【嵌入式——QT】QPainter基本绘图

  • QPainter与QPaintDevice
  • paintEvent事件和绘图区
  • QPainter主要属性
  • QPen主要功能
  • QBrush主要功能
  • QPainter绘制基本图形方法
  • 图示
  • 代码示例

QPainter与QPaintDevice

QPainter是用来进行绘图操作的类,QPaintDevice是一个可以使用QPainter进行绘图的抽象的二维界面,QPaintEngine给QPainter提供在不同设备上绘图的接口,QPaintEngine类由QPainter和QPaintDevice内部使用,应用程序一般无需和QPaintEngine打交道,除非要创建自己的设备类型。

paintEvent事件和绘图区

QWidget类及其子类是最常用的绘图设备,从QWidget类继承的类都有paintEvent事件,要在设备上绘图,只需重定义此事件并编写相应代码。

QPainter主要属性

  • pen属性:是一个QPen对象,用于控制线条的颜色、宽度、线型等;
  • brush属性:是一个QBrush对象,用于设置一个区域的填充特性,可以设置填充颜色、填充方式、渐变特性;
  • font属性:是一个QFont对象,用于绘制文字时,设置文字的字体样式、大小等属性;

QPen主要功能

  • setColor(const QColor &color):设置画笔颜色,即线条颜色;
  • setWidth(int width):设置线条宽度;
  • setStyle(Qt::PenStyle style):设置线条样式;
  • setCapStyle(Qt::PenCapStyle style):设置线条端点样式;
  • setJoinStyle(Qt::PenJoinStyle style):设置连接方式;

QBrush主要功能

  • setColor(const QColor &color):设置画刷颜色;
  • setStyle(Qt::BrushStyle style):设置画刷样式;
  • setTexture(const QPixmap &pixmap):设置一个QPixmap类型的图片作为画刷的图片;
  • setTextureImage(const QImage &image):设置一个QImage类型的图片作为画刷的图片;

QPainter绘制基本图形方法

  • drawArc():画弧线;
  • drawChord():画一段弦;
  • drawConvexPolygon():画凸多边形;
  • drawEllipse:画椭圆;
  • drawImage():绘制图片;
  • drawLine(const QLineF &line):画直线;
  • drawLines(const QLineF *lines, int lineCount):画一批直线;
  • drawPath(const QPainterPath &path):绘制由QPainterPath对象定义的路线;
  • drawPie(const QRectF &rectangle, int startAngle, int spanAngle):绘制扇形;
  • drawPixmap(int x, int y, const QPixmap &pixmap):绘制QPixmap类型图片;
  • drawPoint(int x, int y):绘制一个点;
  • drawPoints:绘制一批点;
  • drawPolygon:绘制多边形,最后一个点和第一个点闭合;
  • drawPolyline:绘制多点连接的线,最后一个点不会和第一个点连接;
  • drawRect(int x, int y, int width, int height):画矩形;
  • drawRoundedRect:绘制圆角矩形;
  • drawText(int x, int y, const QString &text):绘制文本,只能绘制单行文字;
  • fillRect(int x, int y, int width, int height, const QBrush &brush):填充一个矩形,无边框线;
  • eraseRect:擦除某个矩形区域;
  • fillPath:填充某个QPainterPath定义的绘图路径 但是轮廓线不显示;
  • fillRect:填充一个矩形,无边框线;

图示

随便截了两张图片,代码中均有实现,不一一列举图片了。
在这里插入图片描述

在这里插入图片描述

代码示例

QPainterForm.h

#ifndef QPAINTERFORM_H
#define QPAINTERFORM_H#include <QWidget>namespace Ui
{class QPainterForm;
}class QPainterForm : public QWidget
{Q_OBJECTpublic:explicit QPainterForm(QWidget* parent = nullptr);~QPainterForm();void paintEvent(QPaintEvent* event) Q_DECL_OVERRIDE;void drawArcDemo();void drawChordDemo();void drawConvexPolygonDemo();void drawEllipseDemo();void drawImageDemo();void drawLineDemo();void drawLinesDemo();void drawPathDemo();void drawPieDemo();void drawPixmapDemo();void drawPointDemo();void drawPointsDemo();void drawPolygonDemo();void drawPolylineDemo();void drawRectDemo();void drawRoundedRectDemo();void dratTextDemo();void eraseRectDemo();void fillPathDemo();void fillRectDemo();void fivePointedStar();void viewPortAndWindow();private:Ui::QPainterForm* ui;
};#endif // QPAINTERFORM_H

QPainterForm.cpp

#include "QPainterForm.h"
#include "ui_QPainterForm.h"
#include <QPainter>
QPainterForm::QPainterForm(QWidget* parent): QWidget(parent), ui(new Ui::QPainterForm)
{ui->setupUi(this);//设置窗口背景为白色setPalette(QPalette(Qt::white));setAutoFillBackground(true);
}QPainterForm::~QPainterForm()
{delete ui;
}void QPainterForm::paintEvent(QPaintEvent* event)
{QPainter painter(this);painter.setRenderHint(QPainter::Antialiasing);painter.setRenderHint(QPainter::TextAntialiasing);//宽int w = this->width();//高int h = this->height();#if 0//中间区域矩形块QRect rect(w/4, h/4, w/2, h/2);//画笔QPen pen;pen.setWidth(3);//线宽pen.setColor(Qt::red);//线条红色pen.setStyle(Qt::DashLine);//线的样式,实线、虚线pen.setCapStyle(Qt::FlatCap);//线的断点样式pen.setJoinStyle(Qt::BevelJoin);//线的连接点样式painter.setPen(pen);//画刷QBrush brush;brush.setColor(Qt::yellow);//画刷颜色//Qt::BrushStylebrush.setStyle(Qt::CrossPattern);//画刷填充样式painter.setBrush(brush);//绘图painter.drawRect(rect);#endif#if 0QPainter painter2(this);/**QRadialGradient(qreal cx, qreal cy, qreal radius, qreal fx, qreal fy);cx xy 辐射填充的中心点,radius 是辐射填充区的半径,fx fy是焦点坐标**/QRadialGradient radialGrad(w/2, h/2, qMax(w/8, h/8), w/2, h/2);//0表示起点radialGrad.setColorAt(0, Qt::green);//1表示终点radialGrad.setColorAt(1, Qt::blue);radialGrad.setSpread(QGradient::ReflectSpread);painter2.setBrush(radialGrad);painter2.drawRect(this->rect());#endif// drawArcDemo();// drawChordDemo();// drawConvexPolygonDemo();// drawEllipseDemo();// drawImageDemo();// drawLineDemo();// drawLinesDemo();// drawPathDemo();// drawPieDemo();// drawPixmapDemo();// drawPointDemo();// drawPointsDemo();// drawPolygonDemo();// drawPolylineDemo();// drawRectDemo();// drawRoundedRectDemo();// dratTextDemo();// eraseRectDemo();// fillPathDemo();// fillRectDemo();// fivePointedStar();viewPortAndWindow();
}void QPainterForm::drawArcDemo()
{//宽int w = this->width();//高int h = this->height();QRect rect(w/4, h/4, w/2, h/2);int startAngle = 90 * 16;int spanAngle = 90*16;QPainter painter(this);//画弧线painter.drawArc(rect, startAngle, spanAngle);
}void QPainterForm::drawChordDemo()
{//宽int w = this->width();//高int h = this->height();QRect rect(w/4, h/4, w/2, h/2);int startAngle = 90 * 16;int spanAngle = 90*16;QPainter painter(this);//画一段弦painter.drawChord(rect, startAngle, spanAngle);
}void QPainterForm::drawConvexPolygonDemo()
{//宽int w = this->width();//高int h = this->height();QPoint points[4] = {QPoint(5*w/12, h/4), QPoint(3*w/4, 5*h/12), QPoint(5*w/12, 3*h/4), QPoint(w/4, 5*h/12)};QPainter painter(this);//根据给定的点画凸多边形painter.drawConvexPolygon(points, 4);
}void QPainterForm::drawEllipseDemo()
{//宽int w = this->width();//高int h = this->height();QRect rect(w/4, h/4, w/2, h/2);QPainter painter(this);//画椭圆painter.drawEllipse(rect);
}void QPainterForm::drawImageDemo()
{//宽int w = this->width();//高int h = this->height();QRect rect(w/4, h/4, w/2, h/2);QImage image("图片地址");QPainter painter(this);//在指定区域内绘制图片painter.drawImage(rect, image);
}void QPainterForm::drawLineDemo()
{//宽int w = this->width();//高int h = this->height();QLine line(w/4, h/4, w/2, h/2);QPainter painter(this);//画直线painter.drawLine(line);
}void QPainterForm::drawLinesDemo()
{//宽int w = this->width();//高int h = this->height();QRect rect(w/4, h/4, w/2, h/2);QVector<QLine> lines;lines.append(QLine(rect.topLeft(), rect.bottomRight()));lines.append(QLine(rect.topRight(), rect.bottomLeft()));lines.append(QLine(rect.topLeft(), rect.bottomLeft()));lines.append(QLine(rect.topRight(), rect.bottomRight()));QPainter painter(this);//画一批直线painter.drawLines(lines);
}void QPainterForm::drawPathDemo()
{//宽int w = this->width();//高int h = this->height();QRect rect(w/4, h/4, w/2, h/2);QPainterPath path;path.addEllipse(rect);path.addRect(rect);QPainter painter(this);//绘制由QPainterPath定义的路线painter.drawPath(path);
}void QPainterForm::drawPieDemo()
{//宽int w = this->width();//高int h = this->height();QRect rect(w/4, h/4, w/2, h/2);int startAngle = 40*16;int spanAngle = 120*16;QPainter painter(this);//绘制扇形painter.drawPie(rect, startAngle, spanAngle);
}void QPainterForm::drawPixmapDemo()
{//宽int w = this->width();//高int h = this->height();QRect rect(w/4, h/4, w/2, h/2);QPixmap pixmap("图片地址");QPainter painter(this);//绘制QPixmap类型的图片painter.drawPixmap(rect, pixmap);
}void QPainterForm::drawPointDemo()
{//宽int w = this->width();//高int h = this->height();QPainter painter(this);//绘制一个点painter.drawPoint(QPoint(w/2, h/2));
}void QPainterForm::drawPointsDemo()
{//宽int w = this->width();//高int h = this->height();QPoint points[] = {QPoint(5*w/12, h/4), QPoint(3*w/4, 5*h/12), QPoint(2*w/4, 5*h/12)};QPainter painter(this);//绘制一批点painter.drawPoints(points, 3);
}void QPainterForm::drawPolygonDemo()
{//宽int w = this->width();//高int h = this->height();QPoint points[] = {QPoint(5*w/12, h/4), QPoint(3*w/4, 5*h/12), QPoint(5*w/12, 3*h/4), QPoint(2*w/4, 5*h/12)};QPainter painter(this);//绘制多边形,最后一个点和第一个点闭合painter.drawPolygon(points, 4);
}void QPainterForm::drawPolylineDemo()
{//宽int w = this->width();//高int h = this->height();QPoint points[] = {QPoint(5*w/12, h/4), QPoint(3*w/4, 5*h/12), QPoint(5*w/12, 3*h/4), QPoint(2*w/4, 5*h/12)};QPainter painter(this);//绘制多点连接的线,最后一个点不会和第一个点连接painter.drawPolyline(points, 4);
}void QPainterForm::drawRectDemo()
{//宽int w = this->width();//高int h = this->height();QRect rect(w/4, h/4, w/2, h/2);QPainter painter(this);//绘制矩形painter.drawRect(rect);
}void QPainterForm::drawRoundedRectDemo()
{//宽int w = this->width();//高int h = this->height();QRect rect(w/4, h/4, w/2, h/2);QPainter painter(this);//绘制圆角矩形painter.drawRoundedRect(rect, 20, 20);
}void QPainterForm::dratTextDemo()
{//宽int w = this->width();//高int h = this->height();QRect rect(w/4, h/4, w/2, h/2);QFont font;font.setPointSize(30);font.setBold(true);QPainter painter(this);painter.setFont(font);//绘制文本painter.drawText(rect, "Hello Qt");
}void QPainterForm::eraseRectDemo()
{//宽int w = this->width();//高int h = this->height();QRect rect(w/4, h/4, w/2, h/2);QPainter painter(this);//擦除某个矩形区域painter.eraseRect(rect);
}void QPainterForm::fillPathDemo()
{//宽int w = this->width();//高int h = this->height();QRect rect(w/4, h/4, w/2, h/2);QPainterPath path;path.addEllipse(rect);path.addRect(rect);QPainter painter(this);//填充某个QPainterPath定义的绘图路径 但是轮廓线不显示painter.fillPath(path, Qt::red);
}void QPainterForm::fillRectDemo()
{//宽int w = this->width();//高int h = this->height();QRect rect(w/4, h/4, w/2, h/2);QPainter painter(this);//填充一个矩形,无边框线painter.fillRect(rect, Qt::green);
}
//坐标变换 五角星
void QPainterForm::fivePointedStar()
{QPainter painter(this);painter.setRenderHint(QPainter::Antialiasing);painter.setRenderHint(QPainter::TextAntialiasing);qreal r = 100;const qreal pi = 3.14159;qreal deg = pi*72/180;QPoint points[5]= {QPoint(r, 0),QPoint(r*std::cos(deg), -r*std::sin(deg)),QPoint(r*std::cos(2*deg), -r*std::sin(2*deg)),QPoint(r*std::cos(3*deg), -r*std::sin(3*deg)),QPoint(r*std::cos(4*deg), -r*std::sin(4*deg))};//设置字体QFont font;font.setPointSize(12);font.setBold(true);painter.setFont(font);//设置画笔QPen penLine;penLine.setWidth(2);//线宽penLine.setColor(Qt::blue); //线颜色penLine.setStyle(Qt::SolidLine); //线的类型penLine.setCapStyle(Qt::FlatCap);//线端点样式penLine.setJoinStyle(Qt::BevelJoin);//线连接点样式painter.setPen(penLine);//设置画刷QBrush brush;brush.setColor(Qt::yellow);brush.setStyle(Qt::SolidPattern);painter.setBrush(brush);QPainterPath starPath;starPath.moveTo(points[0]);starPath.lineTo(points[2]);starPath.lineTo(points[4]);starPath.lineTo(points[1]);starPath.lineTo(points[3]);starPath.closeSubpath();//闭合路径,最后一个点与第一个点相连starPath.addText(points[0], font, "0");starPath.addText(points[1], font, "1");starPath.addText(points[2], font, "2");starPath.addText(points[3], font, "3");starPath.addText(points[4], font, "4");//绘图painter.save();//保存坐标状态painter.translate(100, 120); //平移painter.drawPath(starPath);//画星星painter.drawText(0, 0, "S1");painter.restore();//恢复坐标状态painter.translate(300, 120); //平移painter.scale(0.8, 0.8); //缩放painter.rotate(90);//顺时针旋转90度painter.drawPath(starPath);painter.drawText(0, 0, "S2");painter.resetTransform();//复位所有坐标变换painter.translate(500, 120); //平移painter.rotate(-145);//逆时针旋转145度painter.drawPath(starPath);painter.drawText(0, 0, "S3");
}//视口 窗口
void QPainterForm::viewPortAndWindow()
{QPainter painter(this);//宽int w = this->width();//高int h = this->height();int side =qMin(w, h);QRect rect((w-side)/2, (h-side)/2, side, side);painter.drawRect(rect);painter.setViewport(rect);//设置viewport 图案就在这个矩形框之内painter.setWindow(-100, -100, 200, 200);painter.setRenderHint(QPainter::Antialiasing);QPen pen;pen.setWidth(1);pen.setColor(Qt::red);pen.setStyle(Qt::SolidLine);painter.setPen(pen);for(int i=0; i<36; i++) {//画椭圆painter.drawEllipse(QPoint(50, 0), 50, 50);//顺时针旋转10度painter.rotate(10);}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/537921.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

EI级 | Matlab实现PCA-GCN主成分降维结合图卷积神经网络的数据多特征分类预测

EI级 | Matlab实现PCA-GCN主成分降维结合图卷积神经网络的数据多特征分类预测 目录 EI级 | Matlab实现PCA-GCN主成分降维结合图卷积神经网络的数据多特征分类预测效果一览基本介绍程序设计参考资料 效果一览 基本介绍 1.Matlab实现PCA-GCN主成分降维结合图卷积神经网络的数据多…

算法执行顺序(科拉多·伯姆,朱塞佩·贾可皮尼)

一.三大算法&#xff08;逻辑跳转&#xff09; 二.break 与 continue ---杨同学---

线程工具类与原子类

参考文档&#xff1a; CountDownLatch、CyclicBarrier、Semaphore的用法和区别juc15_基本AtomicInteger、数组、引用AtomicStampedReference、对象的属性修改原子类 AtomicIntegerFieldUp 、原子操作增强类LongAdder 辅助工具类 CountDownLatch(闭锁) 做减法 允许一个或多个…

西瓜书机器学习AUC与ℓ-rank(loss)的联系理解以及证明(通俗易懂)

前言 在学习到这部分时&#xff0c;对 ℓ-rank 以及AUC的关系难以理解透彻&#xff0c;在网上看到其他博主也并未弄明白&#xff0c;大家大多写自己的理解&#xff0c;我希望您在看完这篇文章时能够深刻理解这二者的关系&#xff0c;如果我的理解有误&#xff0c;希望您在评论…

Maven的基本使用

Maven的基本使用 一、为什么要使用Maven1.1 方便添加第三方Jar包1.2 方便获取第三方Jar包(中央仓库)1.3 解决了依赖问题1.4 解决了依赖冲突1.4 项目管理更清晰1.5 总结 二、Maven的下载三、配置Maven本地仓库和中央仓库3.1 Settings文件3.2 配置本地仓库3.3 配置中央仓库(镜像地…

已经连接过的网络的密码忘记了,怎么快速找回?

使用笔记本电脑曾经连接过一些无线路由器&#xff0c;时间久了&#xff0c;密码可能就忘记了。再使用其他设备连接时&#xff0c;就需要尝试去找到这个密码。本片文章就是通过几个命令快速找到之前使用笔记本电脑曾经连接过的无线网络的密码。 第一步、查看曾经连接过哪些无线网…

2024年将人力RPO项目当蓝海项目吗?

随着科技的快速发展和全球化趋势的加强&#xff0c;人力资源外包(RPO)项目在过去的几年中异军突起&#xff0c;成为企业优化人力资源配置、降低运营成本的重要手段。然而&#xff0c;到了2024年&#xff0c;我们是否还能将人力RPO项目视为一片尚待开发的蓝海呢? 从市场角度来看…

膝关节痛做什么锻炼好得快?这些运动对膝关节很“友好”!

膝关节不是身体中最常受伤的部位&#xff0c;却是最薄弱、要求最高的关节&#xff0c;原因是膝关节承受人的整个重量&#xff0c;而且由于活动范围大&#xff0c;极容易造成膝关节疼痛。 膝关节疼痛多发生于青年人&#xff0c;如田径、登山运动员&#xff0c;舞蹈演员等。发病率…

蓝桥杯算法错题记录-基础篇

文章目录 本文还在跟新&#xff0c;最新跟新时间3/11&#xff01;&#xff01;&#xff01; 格式一定要符合要求&#xff0c;&#xff08;输入&#xff0c;输出格式&#xff09;1. nextInt () next() nextLine() 的注意事项2 .数的幂 a^2等3.得到最大长度&#xff08;最大...&a…

Gitee的注册和代码提交(附有下载链接)

目录 一、Git的下载和安装二、安装图形化界面工具三、在Gitee上创建仓库四、如何把仓库开源五、Clone远程仓库到本地六、拷贝代码到本地的仓库七、Add-Commit-Push到远程仓库八、可能出现的问题8.1 建议在本地仓库直接创建项目8.2 第一次Push可能出现的问题8.3 怎么删除Gitee上…

挑战杯 机器学习股票大数据量化分析与预测系统 - python 挑战杯

文章目录 0 前言1 课题背景2 实现效果UI界面设计web预测界面RSRS选股界面 3 软件架构4 工具介绍Flask框架MySQL数据库LSTM 5 最后 0 前言 &#x1f525; 优质竞赛项目系列&#xff0c;今天要分享的是 &#x1f6a9; 机器学习股票大数据量化分析与预测系统 该项目较为新颖&am…

悲观锁和乐观锁

如果将悲观锁&#xff08;Pessimistic Lock&#xff09;和乐观锁&#xff08;PessimisticLock 或 OptimisticLock&#xff09;对应到现实生活中来。悲观锁有点像是一位比较悲观&#xff08;也可以说是未雨绸缪&#xff09;的人&#xff0c;总是会假设最坏的情况&#xff0c;避免…