Results 1 to 5 of 5

Thread: can't get format under cursor of a QTextEdit

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #4
    Join Date
    Oct 2011
    Posts
    1
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: can't get format under cursor of a QTextEdit

    QSyntaxHighlighter does not change the character format directly in the QTextEdit. You cannot retrieve the format via QTextCursror::charFormat. QSyntaxHighlighter sets the format in additionalFormats as part of the block layout. You can get a list of all formats used in the block the cursor is in by using textCursor().block().layout()->additionalFormats(). This will be a list of FormatRange objects. Get the cursor position in the block using textCursor().positionInBlock() and loop over the items in additionalFormats checking if the cursor is within the item's range (the start of is relative within the block) and if the format matches what you're looking for.

    Here is an excerpt from a project I'm working on where I'm checking the formatting set by QSyntaxHighlighter for offering spell checking suggestions.

    Qt Code:
    1. QTextCursor c = textCursor();
    2. int pos = c.positionInBlock();
    3. foreach (QTextLayout::FormatRange r, textCursor().block().layout()->additionalFormats()) {
    4. if (pos >= r.start && pos <= r.start + r.length && r.format.underlineStyle() == QTextCharFormat::SpellCheckUnderline) {
    5. c.setPosition(c.block().position() + r.start);
    6. c.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, r.length);
    7. setTextCursor(c);
    8. offerSpelling = true;
    9. break;
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by user_none; 29th October 2011 at 13:19.

Similar Threads

  1. QTextEdit Cursor
    By chris_helloworld in forum Qt Programming
    Replies: 4
    Last Post: 18th November 2019, 03:09
  2. QTextEdit current cursor text format
    By afail in forum Qt Programming
    Replies: 0
    Last Post: 13th April 2009, 14:24
  3. QTextEdit and cursor
    By newplayer in forum Qt Programming
    Replies: 1
    Last Post: 30th July 2008, 23:24
  4. Size of the cursor in QTextEdit
    By Ankitha Varsha in forum Qt Programming
    Replies: 3
    Last Post: 22nd January 2008, 12:32
  5. How to synchronize text cursor and cursor in QTextEdit
    By kennyxing in forum Qt Programming
    Replies: 6
    Last Post: 18th February 2007, 09:14

Tags for this Thread

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.