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

news/2024/10/18 23:26:36/

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;
}

源码下载


http://www.ppmy.cn/news/1450420.html

相关文章

boost::asio::ip::tcp::resolver async_resolve异步解析

boost::asio::ip::tcp::resolver::async_resolve 是一个异步解析函数&#xff0c;用于将主机名和服务名转换为端点&#xff08;通常是 IP 地址和端口号&#xff09;。 以下是一个简单的使用示例&#xff1a; #include <boost/asio.hpp> #include <iostream>void …

万兴PDF专家 PDFelement Pro v10.3.8 破姐版!

&#x1f9d1;‍&#x1f4bb;万兴PDF专家 PDFelement Pro v10.3.8 破姐版 (https://docs.qq.com/sheet/DRVVxTHJ3RXJFVHVr)

基于 Wireshark 分析 IP 协议

一、IP 协议 IP&#xff08;Internet Protocol&#xff09;协议是一种网络层协议&#xff0c;它用于在计算机网络中实现数据包的传输和路由。 IP协议的主要功能有&#xff1a; 1. 数据报格式&#xff1a;IP协议将待传输的数据分割成一个个数据包&#xff0c;每个数据包包含有…

AcWing 3194:最大的矩形 ← 笛卡尔树

【题目来源】https://www.acwing.com/problem/content/3197/【题目描述】 在横轴上放了 n 个相邻的矩形&#xff0c;每个矩形的宽度是 1&#xff0c;而第 i&#xff08;1≤i≤n&#xff09;个矩形的高度是 hi。 这 n 个矩形构成了一个直方图。 例如&#xff0c;下图中六个矩形的…

K8s: Helm搭建mongodb集群(1)

mongodb 集群搭建 mongdb 部署前 需要创建 pvc, pv 和 sc&#xff0c;如果在云上会自动创建helm 应用中心: https://artifacthub.io 1 &#xff09;Helm 安装 mongodb A. 无本地存储配置&#xff0c;重启数据消失 在 https://artifacthub.io/packages/helm/bitnami/mongodb…

git 清除已提交的记录

git 清除已提交的记录 步骤一 首先确保你本地没有做任何更改 提交你的当前更改&#xff1a; bashCopy codegit add . git commit -m "Committing current changes"执行 rebase 命令&#xff1a; bash Copy code git rebase -i HEAD~2如果你不想保留当前更改&#xf…

C++_set和map的学习

1. 关联式容器 STL中的容器有序列式容器和关联式容器。 其中 vector 、 list 、 deque 、 forward_list(C11)就是序列式容器&#xff0c; 因为其底层为线性序列的数据结构&#xff0c;里面 存储的是元素本身 关联式容器 也是用来存储数据的&#xff0c;与序列式容器不同的是&am…

Word文件后缀

Word文件后缀 .docx文件为Microsoft Word文档后缀名&#xff0c;基于XML文件格式 .dotm为Word启用了宏的模板 .dotx为Word模板 .doc为Word97-2003文档&#xff0c;二进制文件格式 参考链接 Word、Excel 和 PowerPoint 的文件格式参考 Learn Microsoft