《QT实用小工具·四十四》支持图片和动图的文本编辑器

2024-04-28 06:12

本文主要是介绍《QT实用小工具·四十四》支持图片和动图的文本编辑器,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1、概述
源码放在文章末尾

该项目实现了一个功能丰富的文本编辑器,除了包含文本常规的编辑功能,还包括图片的插入功能和动图的插入功能,项目demo演示如下所示:
在这里插入图片描述

项目部分代码如下所示:

#include "imagehelper.h"#include <QFile>
#include <QFileInfo>
#include <QQmlFile>
#include <QQuickTextDocument>
#include <QDebug>Api::Api(QObject *parent): QObject(parent)
{
}bool Api::exists(const QString &arg)
{return QFile::exists(arg);
}QString Api::baseName(const QString &arg)
{return QFileInfo(arg).baseName();
}ImageHelper::ImageHelper(QObject *parent): QObject(parent),m_maxWidth(120),m_maxHeight(120)
{}ImageHelper::~ImageHelper()
{cleanup();
}void ImageHelper::insertImage(const QUrl &url)
{QImage image = QImage(QQmlFile::urlToLocalFileOrQrc(url));if (image.isNull()){qDebug() << "不支持的图像格式";return;}QString filename = url.toString();QString suffix = QFileInfo(filename).suffix();if (suffix == "GIF" || suffix == "gif") //如果是gif,则单独处理{QString gif = filename;if (gif.left(4) == "file")gif = gif.mid(8);else if (gif.left(3) == "qrc")gif = gif.mid(3);textCursor().insertHtml("<img src='" + url.toString() + "' width = " +QString::number(qMin(m_maxWidth, image.width())) + " height = " +QString::number(qMin(m_maxHeight, image.height()))+ "/>");textDocument()->addResource(QTextDocument::ImageResource, url, image);if (m_urls.contains(url))return;else{QMovie *movie = new QMovie(gif);movie->setCacheMode(QMovie::CacheNone);connect(movie, &QMovie::finished, movie, &QMovie::start);   //循环播放connect(movie, &QMovie::frameChanged, this, [url, this](int){QMovie *movie = qobject_cast<QMovie *>(sender());textDocument()->addResource(QTextDocument::ImageResource, url, movie->currentPixmap());emit needUpdate();});m_urls[url] = movie;movie->start();}}else{        QTextImageFormat format;format.setName(filename);format.setWidth(qMin(m_maxWidth, image.width()));format.setHeight(qMin(m_maxHeight, image.height()));textCursor().insertImage(format, QTextFrameFormat::InFlow);}
}void ImageHelper::cleanup()
{for (auto it : m_urls)it->deleteLater();m_urls.clear();
}QQuickTextDocument* ImageHelper::document() const
{return  m_document;
}void ImageHelper::setDocument(QQuickTextDocument *document)
{if (document != m_document){m_document = document;emit documentChanged();}
}int ImageHelper::cursorPosition() const
{return m_cursorPosition;
}void ImageHelper::setCursorPosition(int position)
{if (position != m_cursorPosition){m_cursorPosition = position;emit cursorPositionChanged();}
}int ImageHelper::selectionStart() const
{return m_selectionStart;
}void ImageHelper::setSelectionStart(int position)
{if (position != m_selectionStart){m_selectionStart = position;emit selectionStartChanged();}
}int ImageHelper::selectionEnd() const
{return m_selectionEnd;
}void ImageHelper::setSelectionEnd(int position)
{if (position != m_selectionEnd){m_selectionEnd = position;emit selectionEndChanged();}
}int ImageHelper::maxWidth() const
{return m_maxWidth;
}void ImageHelper::setMaxWidth(int max)
{if (max != m_maxWidth){m_maxWidth = max;emit maxWidthChanged();}
}int ImageHelper::maxHeight() const
{return m_maxHeight;
}void ImageHelper::setMaxHeight(int max)
{if (max != m_maxHeight){m_maxHeight = max;emit maxHeightChanged();}
}QTextDocument* ImageHelper::textDocument() const
{if (m_document)return m_document->textDocument();else return nullptr;
}QTextCursor ImageHelper::textCursor() const
{QTextDocument *doc = textDocument();if (!doc)return QTextCursor();QTextCursor cursor = QTextCursor(doc);if (m_selectionStart != m_selectionEnd){cursor.setPosition(m_selectionStart);cursor.setPosition(m_selectionEnd, QTextCursor::KeepAnchor);}else{cursor.setPosition(m_cursorPosition);}return cursor;
}

源码下载

这篇关于《QT实用小工具·四十四》支持图片和动图的文本编辑器的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Python实现微信自动锁定工具

《Python实现微信自动锁定工具》在数字化办公时代,微信已成为职场沟通的重要工具,但临时离开时忘记锁屏可能导致敏感信息泄露,下面我们就来看看如何使用Python打造一个微信自动锁定工具吧... 目录引言:当微信隐私遇到自动化守护效果展示核心功能全景图技术亮点深度解析1. 无操作检测引擎2. 微信路径智能获

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义

Java中的工具类命名方法

《Java中的工具类命名方法》:本文主要介绍Java中的工具类究竟如何命名,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录Java中的工具类究竟如何命名?先来几个例子几种命名方式的比较到底如何命名 ?总结Java中的工具类究竟如何命名?先来几个例子JD

Android使用ImageView.ScaleType实现图片的缩放与裁剪功能

《Android使用ImageView.ScaleType实现图片的缩放与裁剪功能》ImageView是最常用的控件之一,它用于展示各种类型的图片,为了能够根据需求调整图片的显示效果,Android提... 目录什么是 ImageView.ScaleType?FIT_XYFIT_STARTFIT_CENTE

关于MongoDB图片URL存储异常问题以及解决

《关于MongoDB图片URL存储异常问题以及解决》:本文主要介绍关于MongoDB图片URL存储异常问题以及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录MongoDB图片URL存储异常问题项目场景问题描述原因分析解决方案预防措施js总结MongoDB图

python实现svg图片转换为png和gif

《python实现svg图片转换为png和gif》这篇文章主要为大家详细介绍了python如何实现将svg图片格式转换为png和gif,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录python实现svg图片转换为png和gifpython实现图片格式之间的相互转换延展:基于Py

使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)

《使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)》PPT是一种高效的信息展示工具,广泛应用于教育、商务和设计等多个领域,PPT文档中常常包含丰富的图片内容,这些图片不仅提升了... 目录一、引言二、环境与工具三、python 提取PPT背景图片3.1 提取幻灯片背景图片3.2 提取

C++如何通过Qt反射机制实现数据类序列化

《C++如何通过Qt反射机制实现数据类序列化》在C++工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作,所以本文就来聊聊C++如何通过Qt反射机制实现数据类序列化吧... 目录设计预期设计思路代码实现使用方法在 C++ 工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作。由于数据类

Python实现图片分割的多种方法总结

《Python实现图片分割的多种方法总结》图片分割是图像处理中的一个重要任务,它的目标是将图像划分为多个区域或者对象,本文为大家整理了一些常用的分割方法,大家可以根据需求自行选择... 目录1. 基于传统图像处理的分割方法(1) 使用固定阈值分割图片(2) 自适应阈值分割(3) 使用图像边缘检测分割(4)

MySql match against工具详细用法

《MySqlmatchagainst工具详细用法》在MySQL中,MATCH……AGAINST是全文索引(Full-Textindex)的查询语法,它允许你对文本进行高效的全文搜素,支持自然语言搜... 目录一、全文索引的基本概念二、创建全文索引三、自然语言搜索四、布尔搜索五、相关性排序六、全文索引的限制七