Qt实用技巧:QPainterPath绘图路径(多次画同样的图形集合)

简介: Qt实用技巧:QPainterPath绘图路径(多次画同样的图形集合)

若该文为原创文章,未经允许不得转载

原博主博客地址:http://blog.csdn.net/qq21497936

原博主博客导航:https://blog.csdn.net/qq21497936/article/details/102478062

本文章博客地址:http://blog.csdn.net/qq21497936/article/details/78475963

各位读者,知识无穷而人力有穷,要么改需求,要么找专业人士,要么自己研究

红胖子(红模仿)的博文大全:开发技术集合(包含Qt实用技术、树莓派、三维、OpenCV、OpenGL、ffmpeg、OSG、单片机、软硬结合等等)持续更新中…(点击传送门)

Qt开发专栏:实用技巧(点击传送门)

 

需求

       根据配置文件,可不改变程序只调整配置文件可调整主页面上的字符串。

原理

       1.读取文件,固定格式(文件在本文章中省略)

       2.写一串字符,使用QPainterPath

       3.注意QPainter的时候,需要设置QPen和QBrush

代码

第一部分代码,保存一个QPainterPath

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    // init a shapeItem
    QColor color(Qt::red);
    QFont font("黑体", 24, QFont::Bold, true);
    QPoint point(200,200);
    QString str = "测试QPaintPath";
    QPainterPath painterPath;
    painterPath.addText(QPointF(0,0),
                        QFont("黑体", 24, QFont::Bold, true),
                        tr("测试QPaintPath"));
    _pShapeItem = new ShapeItem();
    _pShapeItem->setColor(color);
    _pShapeItem->setFont(font);
    _pShapeItem->setPosition(point);
    _pShapeItem->setText(str);
    _pShapeItem->setPainterPath(painterPath);
}

第二部分代码,绘制出图形

void MainWindow::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    // 保存之前的painter
    painter.save();
    // 设置该屏幕坐标点为坐标系原点(0,0) 注意:必须要重新设置画笔和画刷
    painter.setPen(_pShapeItem->getColor());         // 字体外部轮廓线条
    painter.setBrush(_pShapeItem->getColor());       // 字体轮廓内部颜色
    painter.translate(_pShapeItem->getPosition());   // 写字体的位置
    painter.drawPath(_pShapeItem->getPainterPath()); // 画
    // 回复之前的painter
    painter.restore();
}

第三部分代码,头文件ShapeItem.h

#ifndef SHAPEITEM_H
#define SHAPEITEM_H
#include <QPainterPath>
#include <QColor>
#include <QFont>
class QPainterPath;
class QPoint;
class QColor;
class QString;
class QFont;
class ShapeItem
{
public:
    ShapeItem();
public:
    void setPainterPath(QPainterPath &path) { _path     = path;     }
    void setPosition(QPoint &position)      { _position = position; }
    void setColor(QColor &color)            { _color    = color;    }
    void setText(QString &text)             { _text     = text;     }
    void setFont(QFont &font)               { _font     = font;     }
public:
    QPainterPath getPainterPath() const { return _path;     }
    QPoint       getPosition()    const { return _position; }
    QColor       getColor()       const { return _color;    }
    QString      getText()        const { return _text;     }
    QFont        getFont()        const { return _font;     }
private:
    QPainterPath _path;
    QPoint _position;
    QColor _color;
    QString _text;
    QFont _font;
};
#endif // SHAPEITEM_H

第四部分代码,源文件ShapeItem.cpp

#include "shapeitem.h"
ShapeItem::ShapeItem()
{
}

 

原博主博客地址:http://blog.csdn.net/qq21497936

原博主博客导航:https://blog.csdn.net/qq21497936/article/details/102478062

本文章博客地址:http://blog.csdn.net/qq21497936/article/details/78475963

 


相关文章
|
4月前
|
存储
QT图形视图框架绘制曲线图和Smith图
QT图形视图框架绘制曲线图和Smith图
79 0
|
4月前
|
数据可视化 图形学 C++
C++ Qt开发:Charts绘图组件概述
Qt 是一个跨平台C++图形界面开发库,利用Qt可以快速开发跨平台窗体应用程序,在Qt中我们可以通过拖拽的方式将不同组件放到指定的位置,实现图形化开发极大的方便了开发效率,本章将重点介绍`QCharts`二维绘图组件的常用方法及灵活运用。Qt Charts 提供了一个强大且易于使用的工具集,用于在 Qt 应用程序中创建各种类型的图表和图形可视化,该模块提供了多种类型的图表,包括折线图、散点图、条形图、饼图等。这使得开发人员能够轻松地将数据以直观的方式呈现给用户,增强应用程序的可视化效果。
161 0
C++ Qt开发:Charts绘图组件概述
|
4月前
|
数据可视化 图形学 开发者
【Qt 底层机制之图形渲染引擎】深入理解 Qt 的 渲染机制:从基础渲染到高级图形
【Qt 底层机制之图形渲染引擎】深入理解 Qt 的 渲染机制:从基础渲染到高级图形
770 4
|
10月前
|
编解码 图形学
29 QT - 绘图设备
29 QT - 绘图设备
34 0
|
1月前
|
容器
【qt】GraphicsView绘图架构
【qt】GraphicsView绘图架构
45 0
|
1月前
【qt】绘图
【qt】绘图
24 0
|
23天前
|
API
Qt绘图之Paint系统
Qt绘图之Paint系统
36 2
|
23天前
Qt绘图之QWidget
Qt绘图之QWidget
38 1
|
1月前
|
XML Linux C++
【Qt】图形化和纯代码实现Hello world的比较
【Qt】图形化和纯代码实现Hello world的比较
|
3月前
Qt绘图(线条、椭圆、矩形、图片滚动)
Qt绘图(线条、椭圆、矩形、图片滚动)
100 3