⚠️ Forum Maintenance: Feb 6th, 8am - 14pm (UTC+2)

Setting user data in an existing QTextBlock

  • I am writing an editor in which I want to store some meta data with certain blocks of text, and QTextBlockUserData seems crafted specifically for this. However, I see no apparent way to set the user data on an existing text block, or add my own text block to a document.

    For example, I want to use QTextBlock::setUserData() on an existing QTextBlock , but the only way I see to access a particular block in a QTextDocument is with a copy:

    QTextBlock	findBlock(int pos) const
    QTextBlock	findBlockByLineNumber(int lineNumber) const
    QTextBlock	findBlockByNumber(int blockNumber) const
    QTextBlock	firstBlock() const

    Then there's QTextCursor where I see QTextBlock block() const, but that also returns a copy. QTextCursor also has several methods to insert a block, but none of them allow you to insert your own object:

    void	insertBlock()
    void	insertBlock(const QTextBlockFormat &format)
    void	insertBlock(const QTextBlockFormat &format, const QTextCharFormat &charFormat)

    I'm at a loss as to how I can either (1) access an existing block inside a document and set the user data or (2) insert my own block in which I've set the user data.

    What am I missing? Thank you!

  • This post is deleted!

  • QTextCursor constructor takes a reference to an existing block. You should be able to change that block passed into the constructor using the QTextCursor methods. You don't need to copy the QTextBlock, AFAICT.

    Since the parameter is const reference, we'll assume that the const-ness is cast away once QTextCursor has a "handle" to the text block. At least that's how it appears to be to me...I've never used those classes.

Log in to reply