Results 1 to 5 of 5

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

  1. #1
    Join Date
    Oct 2011
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question can't get format under cursor of a QTextEdit

    Hi.

    I have a QTextEdit subclass coupled with a QSyntaxHighlighter. After the syntax is highlighted I try to get the char format and the text under a certain position. The text returns correctly, but the format I get seems to be the the default format and not the format I set on the highlighter at this position.
    This is the code:
    Qt Code:
    1. QString LineTextEdit::getTextAndPropertyAtPosition( const QPoint & pos, QTextCharFormat *format)
    2. {
    3. QTextCursor cursor = cursorForPosition(pos);
    4. cursor.select(QTextCursor::WordUnderCursor);
    5. if (format != NULL)
    6. {
    7. *format = cursor.charFormat();
    8. }
    9. QString rc = cursor.selectedText();
    10. cursor.clearSelection();
    11. return rc;
    12. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jun 2011
    Location
    India
    Posts
    14
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

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

    What about using QTextCursor::selection () ..

  3. #3
    Join Date
    Oct 2011
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

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

    The reason I need the character format is because I saved a value in the user property of that format.
    I don't see how I can access that with the selection() method.

  4. #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.

  5. #5
    Join Date
    Oct 2011
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

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

    Thanks. I've found an alternative way of achieving what I wanted in the mean time.
    I'll try your solution later.

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.