欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

嵌入式--QT]QPainter 基本绘图

最编程 2024-03-19 19:14:10
...
#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::BrushStyle brush.setStyle(Qt::CrossPattern);//画刷填充样式 painter.setBrush(brush); //绘图 painter.drawRect(rect); #endif #if 0 QPainter 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.