Results 1 to 10 of 10

Thread: Syntax highlight selection problem

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Default Syntax highlight selection problem

    Hey,

    I have a problem with my syntax highlighting when the user selects some text. It seems that when keywords are selected, non keywords on the same line disappear and vice versa.

    It has something to do with the current line marker, that is created in the overridden paint event of QTextEdit.

    Below is an image with no text selected:

    http://nicky.xept.nl/dennis/s1.png

    Now another image with text selected:

    http://nicky.xept.nl/dennis/s2.png

    For a small project file containing just the issue follow this link:

    http://nicky.xept.nl/dennis/highlight.zip

    The code for the paint event

    Qt Code:
    1. void CCodeWidgetText::paintEvent(QPaintEvent *event)
    2. {
    3. QTextEdit::ExtraSelection highlight;
    4. QList<QTextEdit::ExtraSelection> extras;
    5. QList<QTextCursor*> errorCursors;
    6. QTextCursor originalCursor = this->textCursor();
    7.  
    8.  
    9.  
    10. QTextCursor cursor = this->textCursor();
    11. // SAVE THE SELECTION TO LATER RESTORE IT!
    12. // int start = cursor.selectionStart();
    13. // int end = cursor.selectionEnd();
    14.  
    15. // CLEARING THE SELECTION SOLVES THE HIGHLIGHTING PROBLEM
    16. // BUT IS NOT GOOD
    17. // cursor.clearSelection();
    18.  
    19. // Mark current line
    20. highlight.cursor = this->textCursor();
    21. highlight.format.setProperty(QTextFormat::FullWidthSelection, true);
    22. highlight.format.setBackground(QColor(0,255,0));
    23. extras << highlight;
    24.  
    25. // Add extra selections
    26. this->setExtraSelections(extras);
    27.  
    28. QTextEdit::paintEvent(event);
    29.  
    30. // RESTORING THE SELECTION WILL INTRODUCE THE FAULT! (SAVE FIRST ABOVE)
    31. // cursor.setPosition(start, QTextCursor::MoveAnchor);
    32. // cursor.setPosition(end, QTextCursor::KeepAnchor);
    33. // this->setTextCursor(cursor);
    34. }
    To copy to clipboard, switch view to plain text mode 
    Thanks in advance for any help on this,

    Sander
    Last edited by jpn; 1st June 2008 at 11:30. Reason: missing [code] tags

Similar Threads

  1. QTableView selection problem
    By aiprober in forum Qt Programming
    Replies: 12
    Last Post: 9th June 2008, 11:55
  2. Replies: 1
    Last Post: 1st June 2008, 11:04
  3. Qtreeview selection highlighted problem?
    By thefisher in forum Qt Programming
    Replies: 4
    Last Post: 24th November 2006, 09:50
  4. Selection problem in QTreeView
    By Valheru in forum Qt Programming
    Replies: 3
    Last Post: 7th October 2006, 16:02

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.