Results 1 to 3 of 3

Thread: Higliting matching brackets in QPlainTextEdit

  1. #1
    Join Date
    Apr 2011
    Location
    Russia
    Posts
    7
    Qt products
    Qt4

    Default Higliting matching brackets in QPlainTextEdit

    I have implemented the method of highlighting matching brackets in my class

    Qt Code:
    1. void CodeEditor::cursorPositionChanged()
    2. {
    3. if (!bracketBeginCursor.isNull() || !bracketEndCursor.isNull()) {
    4. bracketBeginCursor.setCharFormat(QTextCharFormat());
    5. bracketEndCursor.setCharFormat(QTextCharFormat());
    6. bracketBeginCursor = bracketEndCursor = QTextCursor();
    7. }
    8.  
    9. QTextCursor cursor = textCursor();
    10.  
    11. int position = cursor.position();
    12.  
    13. if ((!cursor.atBlockEnd() && doc->characterAt(position) == '(') ||
    14. (!cursor.atBlockStart() && doc->characterAt(position - 1) == ')')) {
    15.  
    16. bool forward = doc->characterAt(position) == '(';
    17.  
    18. QTextCursor::MoveOperation move;
    19.  
    20. QChar c, begin, end;
    21.  
    22. if (forward) { //bool forward =
    23. position++;
    24. move = QTextCursor::NextCharacter;
    25. begin = '(';
    26. end = ')';
    27.  
    28. } else {
    29. position -= 2;
    30. move = QTextCursor::PreviousCharacter;
    31. begin = ')';
    32. end = '(';
    33. }
    34.  
    35. bracketBeginCursor = QTextCursor(cursor);
    36. bracketBeginCursor.movePosition(move, QTextCursor::KeepAnchor);
    37.  
    38. QTextCharFormat format = bracketMismatchFormat;
    39.  
    40. int braceDepth = 1;
    41.  
    42. while (!(c = doc->characterAt(position)).isNull()) {
    43. if (c == begin) {
    44. braceDepth++;
    45. } else if (c == end) {
    46. braceDepth--;
    47.  
    48. if (!braceDepth) {
    49. bracketEndCursor = QTextCursor(doc);
    50. bracketEndCursor.setPosition(position);
    51. bracketEndCursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor);
    52. bracketEndCursor.setCharFormat(bracketMatchFormat);
    53.  
    54. format = bracketMatchFormat;
    55.  
    56. break;
    57. }
    58. }
    59. forward ? position++ : position--;
    60. }
    61. bracketBeginCursor.setCharFormat(format);
    62. }
    63. }
    To copy to clipboard, switch view to plain text mode 

    everything works fine, but if the bracket is highlighted and is the first in a block, then the text is placed to the left of it will have the same QTextCharFormat...

    Perhaps there is a link with

    QTextCharFormat QTextCursor::charFormat () const
    Returns the format of the character immediately before the cursor position().
    If the cursor is positioned at the beginning of a text block that is not empty then the format of the character immediately after the cursor is returned.

    Thanks for any advice.

  2. #2
    Join Date
    May 2011
    Posts
    8
    Qt products
    Qt4

    Default Re: Higliting matching brackets in QPlainTextEdit

    (this is an old topic I know)

    One issue of this code is that it changes the undo/redo history of the qplaintextedit. Is there any way to disable the undo/redo while changing the qtextcharformat so it isn't added to the undo stack? Thanks

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

    Default Re: Higliting matching brackets in QPlainTextEdit

    The method described in this thread is outdated. Use QPlainTextEdit::setExtraSelections() instead.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. QRegExp not matching the { character !!
    By gontham in forum Newbie
    Replies: 3
    Last Post: 4th June 2010, 00:20
  2. Matching HTML tags
    By pucara_faa in forum Qt Programming
    Replies: 4
    Last Post: 22nd January 2010, 13:19
  3. no matching function error
    By arpspatel in forum Qt Programming
    Replies: 4
    Last Post: 16th October 2009, 15:47
  4. Algorithms problem of brackets including.
    By luffy27 in forum General Programming
    Replies: 2
    Last Post: 12th April 2007, 01:10
  5. pattern [image] matching
    By rachana in forum Qt Programming
    Replies: 1
    Last Post: 26th February 2007, 13:31

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.