Important: Please read the Qt Code of Conduct -

Merge QTextDocument

  • It will probably be trivial and I just can't find it but is it possible to append a QTextDocument to another?

    I thought I could use for (int blockIter = 0; blockIter< tempDocument.blockCount(); ++blockIter) but there is no insertion method either in QTextDocument or in QTextCursor that accepts QTextBlock

    Thanks in advance

  • Moderators

    Probably the easiest way is to attach a cursor to the document and insert additional data with QTextCursor::insertHtml. Something along the lines of:

    QTextDocument document1, document2;
    QTextCursor cursor(document1);
    // ...

    Kind regards.

  • Yes, that's what I ended up doing, but since my document contains resources I needed to handle them separately and it was a nightmare.
    Digging around it looks like my problem and Bug #9258 are two sides of the same coin

  • Moderators

    To be frank the QTextDocument class (and friends) could use a good rewrite, but I doubt anyone is interested in that. I'm glad you solved your issue, though.


  • Just to solve the issue, even if it's not pretty it works
    To append QTextDocument A to QTextDocument B:

    • every time you add a resource to A you need to keep track of name and type manually
    • add those resources to B making sure you do not run into naming conflict and, if you change a name to a resouce you should amend it's reference in the html code of A
    • add the adjusted htm from A to B using the QTextCursor interface

    @kshegunov said:

    To be frank the QTextDocument class (and friends) could use a good rewrite

    Yes they feel really messy

Log in to reply