代码拉取完成,页面将自动刷新
#include "my_texteditor.h"
#include <QMimeData>
#include <QImageReader>
#include <QFileInfo>
#include <QUrl>
My_TextEditor::My_TextEditor(QWidget *parent) :
QTextEdit(parent)
{
// 调整字体背景色
QPalette palette=this->palette();
palette.setColor(QPalette::Highlight,QColor(22,155,213));
this->setPalette(palette);
}
bool My_TextEditor::canInsertFromMimeData(const QMimeData *source) const
{
return source->hasImage() || source->hasUrls() ||
QTextEdit::canInsertFromMimeData(source);
}
void My_TextEditor::insertFromMimeData(const QMimeData *source)
{
if (source->hasImage())
{
static int i = 1;
QUrl url(QString("dropped_image_%1").arg(i++));
this->dropImage(url, qvariant_cast<QImage>(source->imageData()));
}
else if (source->hasUrls())
{
for(QUrl &url : source->urls())
{
QFileInfo info(url.toLocalFile());
if (QImageReader::supportedImageFormats().contains(
info.suffix().toLower().toLatin1()))
this->dropImage(url, QImage(info.filePath()));
else
emit callOpenFile(url.toLocalFile());
}
}
else
QTextEdit::insertFromMimeData(source);
}
void My_TextEditor::dropImage(const QUrl &url, const QImage &image)
{
if (!image.isNull())
{
int width,height;
width=image.width();
height=image.height();
m_ImageSizeDialog=new ImageSizeDialog();
m_ImageSizeDialog->iniImageSize(&width,&height);
m_ImageSizeDialog->setAttribute(Qt::WA_DeleteOnClose);
m_ImageSizeDialog->exec();
m_ImageSizeDialog=nullptr;
QTextImageFormat ImageFormat;
ImageFormat.setWidth(width);
ImageFormat.setHeight(height);
ImageFormat.setName(url.toString());
this->document()->addResource(QTextDocument::ImageResource, url, image);
this->textCursor().insertImage(ImageFormat);
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。