《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

相关文章

Mybatis对MySQL if 函数的不支持问题解读

《Mybatis对MySQLif函数的不支持问题解读》接手项目后,为了实现多租户功能,引入了Mybatis-plus,发现之前运行正常的SQL语句报错,原因是Mybatis不支持MySQL的if函... 目录MyBATis对mysql if 函数的不支持问题描述经过查询网上搜索资料找到原因解决方案总结Myb

python版本切换工具pyenv的安装及用法

《python版本切换工具pyenv的安装及用法》Pyenv是管理Python版本的最佳工具之一,特别适合开发者和需要切换多个Python版本的用户,:本文主要介绍python版本切换工具pyen... 目录Pyenv 是什么?安装 Pyenv(MACOS)使用 Homebrew:配置 shell(zsh

Qt实现对Word网页的读取功能

《Qt实现对Word网页的读取功能》文章介绍了几种在Qt中实现Word文档(.docx/.doc)读写功能的方法,包括基于QAxObject的COM接口调用、DOCX模板替换及跨平台解决方案,重点讨论... 目录1. 核心实现方式2. 基于QAxObject的COM接口调用(Windows专用)2.1 环境

Java使用Spire.Doc for Java实现Word自动化插入图片

《Java使用Spire.DocforJava实现Word自动化插入图片》在日常工作中,Word文档是不可或缺的工具,而图片作为信息传达的重要载体,其在文档中的插入与布局显得尤为关键,下面我们就来... 目录1. Spire.Doc for Java库介绍与安装2. 使用特定的环绕方式插入图片3. 在指定位

Python+wxPython开发一个文件属性比对工具

《Python+wxPython开发一个文件属性比对工具》在日常的文件管理工作中,我们经常会遇到同一个文件存在多个版本,或者需要验证备份文件与源文件是否一致,下面我们就来看看如何使用wxPython模... 目录引言项目背景与需求应用场景核心需求运行结果技术选型程序设计界面布局核心功能模块关键代码解析文件大

Python多任务爬虫实现爬取图片和GDP数据

《Python多任务爬虫实现爬取图片和GDP数据》本文主要介绍了基于FastAPI开发Web站点的方法,包括搭建Web服务器、处理图片资源、实现多任务爬虫和数据可视化,同时,还简要介绍了Python爬... 目录一. 基于FastAPI之Web站点开发1. 基于FastAPI搭建Web服务器2. Web服务

golang实现nacos获取配置和服务注册-支持集群详解

《golang实现nacos获取配置和服务注册-支持集群详解》文章介绍了如何在Go语言中使用Nacos获取配置和服务注册,支持集群初始化,客户端结构体中的IpAddresses可以配置多个地址,新客户... 目录golang nacos获取配置和服务注册-支持集群初始化客户端可选参数配置new一个客户端 支

利用Python将PDF文件转换为PNG图片的代码示例

《利用Python将PDF文件转换为PNG图片的代码示例》在日常工作和开发中,我们经常需要处理各种文档格式,PDF作为一种通用且跨平台的文档格式,被广泛应用于合同、报告、电子书等场景,然而,有时我们需... 目录引言为什么选择 python 进行 PDF 转 PNG?Spire.PDF for Python

Qt实现删除布局与布局切换功能

《Qt实现删除布局与布局切换功能》在Qt应用开发中,动态管理布局是一个常见需求,比如根据用户操作动态删除某个布局,或在不同布局间进行切换,本文将详细介绍如何实现这些功能,并通过完整示例展示具体操作,需... 目录一、Qt动态删除布局1. 布局删除的注意事项2. 动态删除布局的实现步骤示例:删除vboxLay

基于Python开发Windows自动更新控制工具

《基于Python开发Windows自动更新控制工具》在当今数字化时代,操作系统更新已成为计算机维护的重要组成部分,本文介绍一款基于Python和PyQt5的Windows自动更新控制工具,有需要的可... 目录设计原理与技术实现系统架构概述数学建模工具界面完整代码实现技术深度分析多层级控制理论服务层控制注