Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Setting user data in an existing QTextBlock
Qt 6.11 is out! See what's new in the release blog

Setting user data in an existing QTextBlock

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 1.2k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Wolosocu
    wrote on last edited by Wolosocu
    #1

    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!

    1 Reply Last reply
    1
    • W Offline
      W Offline
      Wolosocu
      wrote on last edited by
      #2
      This post is deleted!
      1 Reply Last reply
      0
      • Kent-DorfmanK Offline
        Kent-DorfmanK Offline
        Kent-Dorfman
        wrote on last edited by
        #3

        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.

        The dystopian literature that served as a warning in my youth has become an instruction manual in my elder years.

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved