C++ 如何将一个QTextDocument的一部分复制到另一个QTextDocument及其所有部分';s格式

C++ 如何将一个QTextDocument的一部分复制到另一个QTextDocument及其所有部分';s格式,c++,qt,qtextdocument,C++,Qt,Qtextdocument,我需要制作一个非常大的QTextDocument(在富文本模式下)的预览版本副本。 因此,我需要类似于clone()函数的东西,但是能够指定限制。 即克隆(int-maxChars)。 我从clone()源代码中了解到,它只是将一个文档作为单个QTextDocumentFragment复制到另一个文档。所以,我不能以我需要的方式修改这个过程 如果使用富文本(通过html标记指定),请执行以下操作: // assuming some QTextDocument named 'source', re

我需要制作一个非常大的QTextDocument(在富文本模式下)的预览版本副本。 因此,我需要类似于
clone()
函数的东西,但是能够指定限制。 即
克隆(int-maxChars)
。 我从
clone()
源代码中了解到,它只是将一个文档作为单个QTextDocumentFragment复制到另一个文档。所以,我不能以我需要的方式修改这个过程

如果使用富文本(通过html标记指定),请执行以下操作:

// assuming some QTextDocument named 'source', return rich text as html QString object
QString html = source.toHtml();

// pass substring to new QTextDocument instance
QTextDocument dest(html.mid(startChar,endChar));
其中startChar和endChar是源QTextDocument中html字符串的整数索引。

如果使用富文本(通过html标记指定),请执行以下操作:

// assuming some QTextDocument named 'source', return rich text as html QString object
QString html = source.toHtml();

// pass substring to new QTextDocument instance
QTextDocument dest(html.mid(startChar,endChar));

其中startChar和endChar是源QTextDocument中html字符串的整数索引。

可能需要执行以下操作:

// assuming some QTextDocument named 'source', return rich text as html QString object
QString html = source.toHtml();

// pass substring to new QTextDocument instance
QTextDocument dest(html.mid(startChar,endChar));
  • 使用
    QTextCursor
    作为父级创建
    QTextCursor
  • 调用
    cursor.movePosition(QTextCursor::Start)
    。这会将光标的位置设置为文档的开头
  • 调用
    cursor.movePosition(QTextCursor::NextWord,QTextCursor::KeepAnchor,n)
    ,其中
    n
    是您希望包含在所选内容中的字数
  • 调用
    cursor.selection()
    。此方法将返回所选的
    QTextDocumentFragment

  • 您可能希望执行以下操作:

    // assuming some QTextDocument named 'source', return rich text as html QString object
    QString html = source.toHtml();
    
    // pass substring to new QTextDocument instance
    QTextDocument dest(html.mid(startChar,endChar));
    
  • 使用
    QTextCursor
    作为父级创建
    QTextCursor
  • 调用
    cursor.movePosition(QTextCursor::Start)
    。这会将光标的位置设置为文档的开头
  • 调用
    cursor.movePosition(QTextCursor::NextWord,QTextCursor::KeepAnchor,n)
    ,其中
    n
    是您希望包含在所选内容中的字数
  • 调用
    cursor.selection()
    。此方法将返回所选的
    QTextDocumentFragment

  • 因为在这种情况下,我需要解析html使其保持有效(处理打开和关闭标记等)。因为在这种情况下,我需要解析html使其保持有效(处理打开和关闭标记等)。嗯,我甚至不知道QTextCursor存在。伙计,Qt几乎每件事都有一门课+1为了清晰和简洁,即。你建议逐字抄写吗?恐怕只有一件事——对于格式复杂的文本(我的情况),这段代码的运行速度会很慢。但是我检查一下,谢谢你的回答@Axilles如果需要按特定数量的字符移动,可以使用
    QTextCursor::Left
    而不是
    QTextCursor::NextWord
    。@user2155932所以要选择预览内容,我需要调用
    cursor.movePosition(QTextCursor::Right,QTextCursor::MoveAnchor,MAX_CHARS)
    ?@Axilles,是的,正确,
    QTextCursor::Left
    实际上会将光标移动到错误的方向=)嗯,甚至不知道QTextCursor的存在。伙计,Qt几乎每件事都有一门课+1为了清晰和简洁,即。你建议逐字抄写吗?恐怕只有一件事——对于格式复杂的文本(我的情况),这段代码的运行速度会很慢。但是我检查一下,谢谢你的回答@Axilles如果需要按特定数量的字符移动,可以使用
    QTextCursor::Left
    而不是
    QTextCursor::NextWord
    。@user2155932所以要选择预览内容,我需要调用
    cursor.movePosition(QTextCursor::Right,QTextCursor::MoveAnchor,MAX_CHARS)
    ?@Axilles,是的,正确,
    QTextCursor::Left
    实际上会将光标移动到错误的方向=)