QT在场景中利用freetype实现独立的文字绘制子类QxFreeTypeTextItem

本文主要是介绍QT在场景中利用freetype实现独立的文字绘制子类QxFreeTypeTextItem,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

QT在场景中利用freetype实现独立的文字绘制子类QxFreeTypeTextItem,继上一章节讲过qt中如何编译freetype

Qt编译和使用freetype矢量字库方法icon-default.png?t=N7T8https://blog.csdn.net/wangningyu/article/details/138927379#QT利用freetype提取字库图片_qt freetype-CSDN博客文章浏览阅读1.2k次。这是某个项目中要用到的片段,结合上一篇文章#QT从字体名获取字库文件路径使用// 保存位图int SaveBitmapToFile(HBITMAP hBitmap, LPSTR lpFileName){HDC hDC;int iBits;WORD wBitCount; DWORD dwPaletteSize=0,dwBmBitsSize,dwDIBSize_qt freetypehttps://blog.csdn.net/wangningyu/article/details/109743104MFC/QT利用COM组件接口从字体名称、粗体、斜体获取到字体文件路径的方法-CSDN博客文章浏览阅读239次。【代码】MFC/QT利用COM组件接口从字体名称、粗体、斜体获取到字体文件路径的方法。https://blog.csdn.net/wangningyu/article/details/138802620

这里将不使用qt默认的QGraphicsSimpleTextItem与QGraphicsTextItem实现一下自定义的文字图形类:

#include "mainwindow.h"
#include "ui_mainwindow.h"#include <QGridLayout>
#include <QPushButton>#include <QGraphicsScene>
#include <QGraphicsItem>
#include <QDebug>
#include <QPainter>
#include <QFont>
#include <QString>
#include <QColor>
#include <QTextDocument>
#include <ft2build.h>
#include FT_FREETYPE_Hclass QxFreeTypeTextItem : public QGraphicsItem
{
public:QxFreeTypeTextItem(const QString& text, int fontSize, const QString& ttfFile, const QColor& textColor, QGraphicsItem* parent = nullptr): QGraphicsItem(parent), m_strText(text), m_iFontSize(fontSize), m_strTtfFile(ttfFile), m_textColor(textColor){m_ftLibrary = NULL;m_ftFace = NULL;if (FT_Init_FreeType(&m_ftLibrary)){qDebug() << "FT_Init_FreeType failed.";return;}if (FT_New_Face(m_ftLibrary, m_strTtfFile.toStdString().c_str(), 0, &m_ftFace)){qDebug() << "FT_New_Face failed: " << m_strTtfFile;return;}FT_Set_Pixel_Sizes(m_ftFace, 0, m_iFontSize);}~QxFreeTypeTextItem(){if(m_ftFace){FT_Done_Face(m_ftFace);m_ftFace = NULL;}if(m_ftLibrary){FT_Done_FreeType(m_ftLibrary);m_ftLibrary = NULL;}}QRectF boundingRect() const override{return m_rect;}void setRect(qreal left, qreal top, qreal width, qreal height){m_rect = QRectF(left, top, width, height);update();}void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = nullptr) override{int x = m_rect.left();int y = m_rect.top();Q_UNUSED(option);Q_UNUSED(widget);// 开始利用freetype绘制文字painter->setRenderHint(QPainter::Antialiasing);painter->setPen(m_textColor);for (int i = 0; i < m_strText.length(); ++i){QChar   ucode = m_strText.at(i);ushort  unicode = ucode.unicode();if (FT_Load_Char(m_ftFace, unicode, FT_LOAD_RENDER)){continue;}FT_GlyphSlot    glyph = m_ftFace->glyph;QImage          glyphImage(glyph->bitmap.width, glyph->bitmap.rows, QImage::Format_ARGB32);glyphImage.fill(Qt::transparent);for (unsigned int j = 0; j < glyph->bitmap.rows; ++j){for (unsigned int k = 0; k < glyph->bitmap.width; ++k){int alpha = glyph->bitmap.buffer[j * glyph->bitmap.width + k];glyphImage.setPixel(k, j, qRgba(m_textColor.red(), m_textColor.green(), m_textColor.blue(), alpha));}}painter->drawImage(QPointF(x + glyph->bitmap_left, y - glyph->bitmap_top), glyphImage);x += (glyph->advance.x >> 6);}}private:QString     m_strText;int         m_iFontSize;QString     m_strTtfFile;QColor      m_textColor;FT_Library  m_ftLibrary;FT_Face     m_ftFace;QRectF      m_rect;
};MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);QGraphicsScene  *pScene = new QGraphicsScene(this);pScene->setSceneRect(QRect(0, 0, 800, 500));ui->graphicsView->setScene(pScene);ui->graphicsView->setRenderHint(QPainter::Antialiasing);//ui->graphicsView->setCacheMode(QGraphicsView::CacheBackground);//ui->graphicsView->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);ui->graphicsView->setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);ui->graphicsView->setMouseTracking(true);ui->graphicsView->setStyleSheet(QString("background:%1").arg(QColor(255, 255, 176).name()));QColor              clr(0, 0, 255);QxFreeTypeTextItem  *pItem = new QxFreeTypeTextItem("FreeType: Hello-Normal by wangningyu", 32, "C:/ArialUMS.TTF", clr);pItem->setRect(10, 10, 300, 100);pItem->setFlag(QGraphicsItem::ItemIsMovable, true);pItem->setFlag(QGraphicsItem::ItemIsSelectable, true);pScene->addItem(pItem);pItem = new QxFreeTypeTextItem("FreeType: Hello-Bold by wangningyu", 32, "C:/Windows/Fonts/Arialbd.ttf", clr);pItem->setRect(10, 50, 300, 100);pItem->setFlag(QGraphicsItem::ItemIsMovable, true);pItem->setFlag(QGraphicsItem::ItemIsSelectable, true);pScene->addItem(pItem);pItem = new QxFreeTypeTextItem("FreeType: Hello-Italic by wangningyu", 32, "C:/Windows/Fonts/Ariali.ttf", clr);pItem->setRect(10, 90, 300, 100);pItem->setFlag(QGraphicsItem::ItemIsMovable, true);pItem->setFlag(QGraphicsItem::ItemIsSelectable, true);pScene->addItem(pItem);pItem = new QxFreeTypeTextItem("FreeType: Hello-Bold-Italic by wangningyu", 32, "C:/Windows/Fonts/Arialbi.ttf", clr);pItem->setRect(10, 130, 300, 100);pItem->setFlag(QGraphicsItem::ItemIsMovable, true);pItem->setFlag(QGraphicsItem::ItemIsSelectable, true);pScene->addItem(pItem);
}MainWindow::~MainWindow()
{delete ui;
}

测试效果:

这篇关于QT在场景中利用freetype实现独立的文字绘制子类QxFreeTypeTextItem的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/994372

相关文章

python生成随机唯一id的几种实现方法

《python生成随机唯一id的几种实现方法》在Python中生成随机唯一ID有多种方法,根据不同的需求场景可以选择最适合的方案,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习... 目录方法 1:使用 UUID 模块(推荐)方法 2:使用 Secrets 模块(安全敏感场景)方法

Spring StateMachine实现状态机使用示例详解

《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命

Spring Boot 结合 WxJava 实现文章上传微信公众号草稿箱与群发

《SpringBoot结合WxJava实现文章上传微信公众号草稿箱与群发》本文将详细介绍如何使用SpringBoot框架结合WxJava开发工具包,实现文章上传到微信公众号草稿箱以及群发功能,... 目录一、项目环境准备1.1 开发环境1.2 微信公众号准备二、Spring Boot 项目搭建2.1 创建

IntelliJ IDEA2025创建SpringBoot项目的实现步骤

《IntelliJIDEA2025创建SpringBoot项目的实现步骤》本文主要介绍了IntelliJIDEA2025创建SpringBoot项目的实现步骤,文中通过示例代码介绍的非常详细,对大家... 目录一、创建 Spring Boot 项目1. 新建项目2. 基础配置3. 选择依赖4. 生成项目5.

nginx -t、nginx -s stop 和 nginx -s reload 命令的详细解析(结合应用场景)

《nginx-t、nginx-sstop和nginx-sreload命令的详细解析(结合应用场景)》本文解析Nginx的-t、-sstop、-sreload命令,分别用于配置语法检... 以下是关于 nginx -t、nginx -s stop 和 nginx -s reload 命令的详细解析,结合实际应

Linux下删除乱码文件和目录的实现方式

《Linux下删除乱码文件和目录的实现方式》:本文主要介绍Linux下删除乱码文件和目录的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux下删除乱码文件和目录方法1方法2总结Linux下删除乱码文件和目录方法1使用ls -i命令找到文件或目录

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并

mybatis执行insert返回id实现详解

《mybatis执行insert返回id实现详解》MyBatis插入操作默认返回受影响行数,需通过useGeneratedKeys+keyProperty或selectKey获取主键ID,确保主键为自... 目录 两种方式获取自增 ID:1. ​​useGeneratedKeys+keyProperty(推

Spring Boot集成Druid实现数据源管理与监控的详细步骤

《SpringBoot集成Druid实现数据源管理与监控的详细步骤》本文介绍如何在SpringBoot项目中集成Druid数据库连接池,包括环境搭建、Maven依赖配置、SpringBoot配置文件... 目录1. 引言1.1 环境准备1.2 Druid介绍2. 配置Druid连接池3. 查看Druid监控

Linux在线解压jar包的实现方式

《Linux在线解压jar包的实现方式》:本文主要介绍Linux在线解压jar包的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux在线解压jar包解压 jar包的步骤总结Linux在线解压jar包在 Centos 中解压 jar 包可以使用 u