Results 1 to 17 of 17

Thread: Accessing the current QTextBlock in QSyntaxHighlighter

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2008
    Posts
    33
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Question Accessing the current QTextBlock in QSyntaxHighlighter

    Hi

    the documentation for

    QSyntaxHighlighter::setCurrentBlockUserData ( QTextBlockUserData * data )
    says

    you can ask the current QTextBlock (retrieved using the QTextCursor::block() function) if it has a user data object set
    but I don't understand how to actually retrieve the current QTextBlock, since on what should I call QTextCursor::block()?

    thanks in advance
    Lorenzo

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Accessing the current QTextBlock in QSyntaxHighlighter

    The text cursor is usually positioned somewhere in the text of QTextDocument. You can move it anywhere you want and you can query for the block it is in.

  3. #3
    Join Date
    Nov 2008
    Posts
    33
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Accessing the current QTextBlock in QSyntaxHighlighter

    Quote Originally Posted by wysota View Post
    The text cursor is usually positioned somewhere in the text of QTextDocument. You can move it anywhere you want and you can query for the block it is in.
    OK, so if I have the position of the cursor I can get the block the cursor is in, but the problem is now: how can I get the current cursor position?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Accessing the current QTextBlock in QSyntaxHighlighter

    I take it that you want to do something with the highlighter... in that case use QSyntaxHighlighter::currentBlockUserData() and forget about the cursor.

  5. #5
    Join Date
    Nov 2008
    Posts
    33
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Accessing the current QTextBlock in QSyntaxHighlighter

    Quote Originally Posted by wysota View Post
    I take it that you want to do something with the highlighter... in that case use QSyntaxHighlighter::currentBlockUserData() and forget about the cursor.
    I don't think that's enough: I need to access the user data of the previous block, because int previousBlockState () const is not enough... and to access the previous block user data I need to access the current block, and through the current block I can get access to the previous block by using QTextBlock::previous()...

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Accessing the current QTextBlock in QSyntaxHighlighter

    Could you say why you need to access the previous block?

  7. #7
    Join Date
    Nov 2008
    Posts
    33
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Accessing the current QTextBlock in QSyntaxHighlighter

    Quote Originally Posted by wysota View Post
    Could you say why you need to access the previous block?
    I'm trying to implement syntax highlighting through GNU Source-highlight and, in case I enter in a specific highlight state (say, e.g., comment state) I need to store such information in the current block's user data (and I can do this now), but I also need to inspect the previous block user data (to know whether we're inside a specific highlight state).

    I don't see any way to access this since, also using TextEdit->cursor()->block() is not enough, since when QSyntaxHighlighter decides that many blocks need to be highlighted TextEdit->cursor()->block() only returns the block where the cursor is, and this is obviously not enough.

    By the way, previousBlockState() is not enough for me, since I need to set specific user data

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Accessing the current QTextBlock in QSyntaxHighlighter

    According to me all you need is the state of the previous block. When you highlight the previous block, you mark its state as "in comment" and then when the next block is being highlighted, you read that value and you know you're still in a comment. What extra data would you like to store, specifically?

  9. #9
    Join Date
    Nov 2008
    Posts
    33
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Accessing the current QTextBlock in QSyntaxHighlighter

    Quote Originally Posted by wysota View Post
    According to me all you need is the state of the previous block. When you highlight the previous block, you mark its state as "in comment" and then when the next block is being highlighted, you read that value and you know you're still in a comment. What extra data would you like to store, specifically?
    I need to store also the stack information: GNU Source-highlight is powerful enough to handle nested highlighting states, using stacks, and only the (integer) state of the previous block is not enough

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Accessing the current QTextBlock in QSyntaxHighlighter

    If it's only a "state" then that's enough. You can use each bit of the integer to mark a state, therefore you can mark a hierarchy of 32 states - for instance "1" means the paragraph is in state "1" and 9 marks it is in states "1" and "4". I doubt you'll ever have more stacks deeper than 32 levels.

    If you can't live with that, simply create a structure which you will index by an integer ("state") that will return the real state associated with a given index.

  11. #11
    Join Date
    Nov 2008
    Posts
    33
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Accessing the current QTextBlock in QSyntaxHighlighter

    Quote Originally Posted by wysota View Post
    If it's only a "state" then that's enough. You can use each bit of the integer to mark a state, therefore you can mark a hierarchy of 32 states - for instance "1" means the paragraph is in state "1" and 9 marks it is in states "1" and "4". I doubt you'll ever have more stacks deeper than 32 levels.

    If you can't live with that, simply create a structure which you will index by an integer ("state") that will return the real state associated with a given index.
    No, I need actual objects: the stack contains references to state objects, so I need to store user data...

    probably the idea of the structure might help

Similar Threads

  1. Qt designer plugin errors on OSX Leopard
    By mpotocnik in forum Qt Tools
    Replies: 10
    Last Post: 21st January 2008, 09:45
  2. Distributing QT application for Mac OS
    By mb0 in forum Qt Programming
    Replies: 1
    Last Post: 31st May 2007, 18:59
  3. Replies: 15
    Last Post: 21st April 2007, 17:46

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.