如何从组件QTextDocument中获得没有元信息的HTML文本



描述

我在QML中创建了一个TextArea组件,与本例类似,我基于指向QQuickTextDocument的指针创建了DocumentHandler类,该指针通过textDocument属性获取。我需要这个,以便能够格式化文本,也就是说,使其粗体,下划线,斜体,删除等

我需要什么

我需要得到一个文本,其中格式化的部分将显示为HTML标记。

例如粗体文本最终我想采用<b>Bold text</b>的形式。或者,例如粗体和斜体文本我希望采用<b><i>Bold and italic text</i></b>格式(标签的放置顺序无关紧要(。

我尝试了什么

我试着使用toHtml((函数,但这个函数不适合我,因为:

  1. 它生成了很多我不需要的不必要的信息。例如,对于粗体文本,它返回以下结果:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
<html><head><meta name="qrichtext" content="1" /><style type="text/css">
p, li { white-space: pre-wrap; }
</style></head><body style=" font-family:'Roboto'; font-size:14px; font-weight:400; font-style:normal;">
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-weight:600;">Bold text</span></p></body></html>
  1. 我需要表示文本的常用标签(<b><i>等(,此函数以<span>标签的style属性的形式形成。因此,它将粗体更改为以下行:<span style=" font-weight:600;">

描述

如果我理解正确的话,目前没有QTextDocument使用toHtml()函数生成的元信息,就无法获得带有HTML标签的格式化文本。因此,我决定使用QTextCursor类手动完成这项工作。

代码

我有一个提供标签信息的结构:

struct Tag
{
Tag() = default;
Tag(const QString& openTag,
const QString& closeTag,
const std::function<bool(const QTextCursor& cursor)>& canBeOpened);
QString getOpenTag() const
{
return m_openTag;
}
QString getCloseTag() const
{
return m_closeTag;
}
bool canBeOpened(const QTextCursor& cursor) const
{
return m_canBeOpened(cursor);
}
private:
QString m_openTag;
QString m_closeTag;
std::function<bool(const QTextCursor&)> m_canBeOpened;
};

我有这样一个结构的std::vector,我初始化如下:

m_tags{ { "<b>", "</b>", [](const QTextCursor& cursor) { return cursor.charFormat().fontWeight() == QFont::Bold; } },
{ "<i>", "</i>", [](const QTextCursor& cursor) { return cursor.charFormat().fontItalic(); } },
{ "<u>", "</u>", [](const QTextCursor& cursor) { return cursor.charFormat().fontUnderline(); } },
{ "<s>", "</s>", [](const QTextCursor& cursor) { return cursor.charFormat().fontStrikeOut(); } } }

最重要的是getFormattedText()函数,它使用Tag对象的这个向量来返回格式化的文本。其主要思想是手动将标记放置在纯文本中,也就是说,开始标记放置在格式开始的地方,结束标记放置在结束的地方。关于文本中使用什么格式的信息可以从QTextCursor类中获取,我们可以基于QTextDocument类创建哪个对象。因此,我们有以下功能:

QString getFormattedText()
{
auto cursor{ textCursor() };
if (!cursor.isNull())
{
int offset{};
auto result{ cursor.document()->toPlainText() };
auto currentTextFormat{ getTextFormat() };
for (int i{}; i < cursor.document()->characterCount(); ++i)
{
cursor.setPosition(i);
const auto localTextFormat{ getTextFormat(cursor) };
if (currentTextFormat != localTextFormat)
{
const auto closedFormat{ getClosedFormat(currentTextFormat) };
const auto openedFormat{ getOpenedFormat(localTextFormat) };
result.insert(i - (i > 0 ? 1 : 0) + offset, closedFormat + openedFormat);
offset += closedFormat.size() + openedFormat.size();
currentTextFormat = localTextFormat;
}
}
result += getClosedFormat(currentTextFormat);
return result.replace("n", "<br>");
}
return {};
}

与CCD_ 18和CCD_;"及时";关闭一个格式组合,然后打开一个新的。这种格式组合被命名为:

using TextFormat = std::vector<std::pair<FontFormat, bool>>;

其中FontFormat为:

enum class FontFormat
{
Bold,
Italic,
Underline,
Strikethrough
};

获取TextFormat:的函数

TextFormat getTextFormat()
{
TextFormat textFormat;
for (const auto& format : m_formats)
{
textFormat.push_back({ format.first, false });
}
return textFormat;
}
TextFormat getTextFormat(const QTextCursor& cursor)
{
TextFormat textFormat;
for (const auto& format : m_formats)
{
textFormat.push_back({ format.first, format.second.canBeOpened(cursor) });
}
return textFormat;
}

获取TextFormat:文本解释的功能

QString getOpenedFormat(const TextFormat& textFormat)
{
const auto append = [](QString& result, const Tag& tag) {
result.push_back(tag.getOpenTag());
};
return getFormat(textFormat, append);
}
QString getClosedFormat(const TextFormat& textFormat)
{
const auto append = [](QString& result, const Tag& tag) {
result.prepend(tag.getCloseTag());
};
return getFormat(textFormat, append);
}
QString getFormat(const TextFormat& textFormat, const std::function<void(QString&, const Tag&)>& append)
{
QString result;
for (const auto& format : textFormat)
{
if (format.second)
{
const auto fndFontFormat{ m_formats.find(format.first) };
if (fndFontFormat != m_formats.end())
{
append(result, fndFontFormat->second);
}
}
}
return result;
}

例如,有这样的文本:a

bc。本文中的每个字母都有不同的格式组合,当从一个字母迭代到另一个字母时,有必要考虑到这一点,关闭旧的组合,打开新的组合。

因此,a

bc将被转换为:<b>a</b><b><s>b</b></s><s>c</s>

最新更新