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

 

 

 

  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值