Здравствуйте!
Нужно подсветить код в QTextEdit с помощью QSyntaxHighlighter. Сколько искал, внятного примера так и не нашел:(
не подскажете как это сделать? если не сложно, то с примером кода.
Заранее спасибо!
class MyProgEdit(QPlainTextEdit):
lastBGcolor=None
MainScreen=None
class PythonHighlighter(QSyntaxHighlighter):
Rules = []
def __init__(self, parent=None):
super(MyProgEdit.PythonHighlighter, self).__init__(parent)
keywordFormat = QTextCharFormat()
keywordFormat.setForeground(Qt.darkBlue)
keywordFormat.setFontWeight(QFont.Bold)
for pattern in ((r"\band\b", r"\bas\b", r"\bassert\b",
r"\bbreak\b", r"\bclass\b", r"\bcontinue\b",
r"\bdef\b", r"\bdel\b", r"\belif\b", r"\belse\b",
r"\bexcept\b", r"\bexec\b", r"\bfinally\b", r"\bfor\b",
r"\bfrom\b", r"\bglobal\b", r"\bif\b", r"\bimport\b",
r"\bin\b", r"\bis\b", r"\blambda\b", r"\bnot\b",
r"\bor\b", r"\bpass\b", r"\bprint\b", r"\braise\b",
r"\breturn\b", r"\btry\b", r"\bwhile\b", r"\bwith\b",
r"\byield\b")):
MyProgEdit.PythonHighlighter.Rules.append((QRegExp(pattern),
keywordFormat))
commentFormat = QTextCharFormat()
commentFormat.setForeground(Qt.darkGreen)
commentFormat.setFontItalic(True)
MyProgEdit.PythonHighlighter.Rules.append((QRegExp(r"#.*"),
commentFormat))
self.stringFormat = QTextCharFormat()
self.stringFormat.setForeground(Qt.red)
stringRe = QRegExp(r"""(?:'[^']*'|"[^"]*")""")
stringRe.setMinimal(True)
MyProgEdit.PythonHighlighter.Rules.append((stringRe, self.stringFormat))
self.stringRe = QRegExp(r"""(:?"["]".*"["]"|'''.*''')""")
self.stringRe.setMinimal(True)
MyProgEdit.PythonHighlighter.Rules.append((self.stringRe,
self.stringFormat))
self.tripleSingleRe = QRegExp(r"""'''(?!")""")
self.tripleDoubleRe = QRegExp(r'''"""(?!')''')
def highlightBlock(self, text):
NORMAL, TRIPLESINGLE, TRIPLEDOUBLE = range(3)
for regex, format in MyProgEdit.PythonHighlighter.Rules:
i = text.indexOf(regex)
while i >= 0:
length = regex.matchedLength()
self.setFormat(i, length, format)
i = text.indexOf(regex, i + length)
self.setCurrentBlockState(NORMAL)
if text.indexOf(self.stringRe) != -1:
return
for i, state in ((text.indexOf(self.tripleSingleRe),
TRIPLESINGLE),
(text.indexOf(self.tripleDoubleRe),
TRIPLEDOUBLE)):
if self.previousBlockState() == state:
if i == -1:
i = text.length()
self.setCurrentBlockState(state)
self.setFormat(0, i + 3, self.stringFormat)
elif i > -1:
self.setCurrentBlockState(state)
self.setFormat(i, text.length(), self.stringFormat)
def __init__(self, txt):
QPlainTextEdit.__init__(self, txt)
font = QFont("Courier New", 10)
font.setFixedPitch(True)
self.setFont(font)
self.highlighter = MyProgEdit.PythonHighlighter(self.document())
self.setSizePolicy(QSizePolicy.Expanding,QSizePolicy.Expanding )
def text(self):
return self.toPlainText()
def event(self, event):
if event.type() == QEvent.KeyPress and \
event.key() == Qt.Key_Tab:
cursor = self.textCursor()
cursor.insertText(" ")
return True
return QPlainTextEdit.event(self, event)
def setText(self, text):
self.setPlainText(text)
def focusInEvent(self, ev):
QPlainTextEdit.focusInEvent(self, ev)
if self.lastBGcolor==None: self.lastBGcolor=self.palette().color(QPalette.Base)
palette = QPalette()
palette.setBrush(QPalette.Active, QPalette.Base, QBrush(ActiveBGcolor))
self.setPalette(palette)
def focusOutEvent(self, ev):
QPlainTextEdit.focusOutEvent(self, ev)
if self.lastBGcolor<>None:
palette = QPalette()
palette.setBrush(QPalette.Active, QPalette.Base, QBrush(self.lastBGcolor))
self.setPalette(palette)