Last active
October 29, 2017 00:12
-
-
Save eyllanesc/9afede5ccad0183b0950d27273dbdc5e to your computer and use it in GitHub Desktop.
Example of QSyntaxHighlighter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import html | |
from PyQt5.QtCore import * | |
from PyQt5.QtGui import * | |
from PyQt5.QtWidgets import * | |
def format(color, style=''): | |
_color = QColor() | |
_color.setNamedColor(color) | |
_format = QTextCharFormat() | |
_format.setForeground(_color) | |
return _format | |
class PythonHighlighter(QSyntaxHighlighter): | |
formats = [('red', 30, 15), ('blue', 60, 125)] | |
def __init__(self, *args, **kwargs): | |
QSyntaxHighlighter.__init__(self, *args, **kwargs) | |
def highlightBlock(self, text): | |
for color, start, length in self.formats: | |
self.setFormat(start, length, format(color)) | |
self.setCurrentBlockState(0) | |
text = """<!DOCTYPE html> | |
<html> | |
<body> | |
<div id="LiveArea">gggg </div> | |
<p><font size="3" | |
color="red">This is some text!</font></p> | |
<p><font size="2" color="blue">This is some text!</font></p> | |
<p><font face="verdana" color="green">This is some text!</font></p> | |
<p><strong>Note:</strong> The font element is not supported in HTML5. Use CSS instead.</p> | |
</body> | |
</html>""" | |
if __name__ == '__main__': | |
import sys | |
app = QApplication(sys.argv) | |
w = QTextEdit() | |
H = PythonHighlighter(w.document()) | |
w.append(html.escape(text)) | |
w.show() | |
sys.exit(app.exec_()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment