首页 > 解决方案 > 如何从组件 QTextDocument 获取没有元信息的 HTML 文本

问题描述

描述

TextArea在 QML 中创建了一个组件,与示例类似,我基于指向 a 的指针创建了一个 DocumentHandler 类,该指针QQuickTextDocument通过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>形式形成。所以它用这一行改变了粗体:.style<span><span style=" font-weight:600;">

标签: qtqmlrichtextqtextdocumentqtextcursor

解决方案


描述

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

代码

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

struct Tag
{
    Tag(const QString& openTag,
        const QString& closeTag,
        const std::function<bool(const QTextCursor& cursor)>& canBeOpened);

    QString getOpenTag() const;
    QString getCloseTag() const;
    bool isOpened() const;
    bool isClosed() const;
    bool canBeOpened(const QTextCursor& cursor) const;
    void open();
    void close();

private:
    std::function<bool(const QTextCursor&)> m_canBeOpened;
    QString m_openTag;
    QString m_closeTag;
    bool m_isOpened{ false };
    bool m_isClosed{ true };
};

我有一个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()
{
    QTextCursor cursor{ textCursor() };
    if (!cursor.isNull())
    {
        QString result{ cursor.document()->toPlainText() };
        for (int i{}, offset{}; i < cursor.document()->characterCount(); ++i)
        {
            cursor.setPosition(i)
            for (auto& tag : m_tags)
            {
                if (tag.canBeOpened(cursor))
                {
                    if (!tag.isOpened())
                    {
                        result.insert(i - (i > 0 ? 1 : 0) + offset, tag.getOpenTag());
                        offset += tag.getOpenTag().size();
                        tag.open();
                    }
                }
                else if (!tag.isClosed())
                {
                    result.insert(i - (i > 0 ? 1 : 0) + offset, tag.getCloseTag());
                    offset += tag.getCloseTag().size();
                    tag.close();
                }
            }
        }
        for (int i = m_tags.size() - 1; i >= 0; --i)
        {
            if (!m_tags[i].isClosed())
            {
                result.insert(result.size(), m_tags[i].getCloseTag());
                m_tags[i].close();
            }
        }
        return result;
    }
    return {};
}

粗体和斜体文本将如下所示<b><i>Bold and italic text</i></b>


推荐阅读