Results 1 to 2 of 2

Thread: SyntaxHighlighter problem

  1. #1
    Join Date
    Jul 2008
    Location
    Poland
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default SyntaxHighlighter problem

    I have found some code of syntax highlight in PyQt4, but my problem is that QTextEdit colorize text, but only first line, and only line when I put enter at the end of line at the following lines. If I open file with code, only first line is colorized. Rest of code is black.

    Would you be so kind to help me?

    Qt Code:
    1. class Highlighter(QtCore.QObject):
    2. def __init__(self, parent=None):
    3. QtCore.QObject.__init__(self, parent)
    4.  
    5. self.mappings = {}
    6.  
    7. def addToDocument(self, doc):
    8. self.connect(doc, QtCore.SIGNAL("contentsChange(int, int, int)"), self.highlight)
    9.  
    10. def addMapping(self, pattern, format):
    11. self.mappings[pattern] = format
    12.  
    13. def highlight(self, position, removed, added):
    14. doc = self.sender()
    15.  
    16. block = doc.findBlock(position)
    17. if not block.isValid():
    18. return
    19.  
    20. endBlock = QtGui.QTextBlock()
    21. if added > removed:
    22. endBlock = doc.findBlock(position + added)
    23. else:
    24. endBlock = block
    25.  
    26. while block.isValid() and not (endBlock < block):
    27. self.highlightBlock(block)
    28. block = block.next()
    29.  
    30. def highlightBlock(self, block):
    31. layout = block.layout()
    32. text = block.text()
    33.  
    34. overrides = []
    35.  
    36. for pattern in self.mappings:
    37. expression = QtCore.QRegExp(pattern)
    38. i = text.indexOf(expression)
    39. while i >= 0:
    40. range = QtGui.QTextLayout.FormatRange()
    41. range.start = i
    42. range.length = expression.matchedLength()
    43. range.format = self.mappings[pattern]
    44. overrides.append(range)
    45.  
    46. i = text.indexOf(expression, i + expression.matchedLength())
    47.  
    48. layout.setAdditionalFormats(overrides)
    49. block.document().markContentsDirty(block.position(), block.length())
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: SyntaxHighlighter problem

    Why not use QSyntaxHighlighter?
    J-P Nurmi

Similar Threads

  1. Weird problem: multithread QT app kills my linux
    By Ishark in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2008, 09:12
  2. Steps in solving a programming problem?
    By triperzonak in forum General Programming
    Replies: 8
    Last Post: 5th August 2008, 08:47
  3. problem with paint and erase in frame
    By M.A.M in forum Qt Programming
    Replies: 9
    Last Post: 4th May 2008, 20:17
  4. Tricky problem with ARGB widget / UpdateLayeredWindow
    By nooky59 in forum Qt Programming
    Replies: 3
    Last Post: 21st February 2008, 10:35
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.