The QSyntaxHighlighter
Applying the QSyntaxHighlighter can be achieved very simply be feeding him with a parent as first argument, like below ;
class StdOut_Syntax(QSyntaxHighlighter): def __init__(self, parent): super(StdOut_Syntax, self).__init__(parent)
PyQt use the function highlightBlock to handle the syntax coloring, this function needs an input text - provided internally by the QTextEdit - in which we'll iterate for each regular expression we want to colorize, define a QTextCharFormat for the expression with the function setFormat, here is a simple example ;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class StdOut_Syntax(QSyntaxHighlighter): def __init__(self, parent): super(StdOut_Syntax, self).__init__(parent) def highlightBlock(self, text): color = QColor(255, 125, 160) pattern = QRegExp(r'^//.+$') # regexp pattern keyword = QTextCharFormat() keyword.setForeground(color) # defining the aspect index = pattern.indexIn(text) while index >= 0: # loop through the text to find matches len = pattern.matchedLength() # length of the match self.setFormat(index, len, keyword) # we apply the format to the match index = pattern.indexIn(text, index + len) self.setCurrentBlockState(0) |
Applying this class to our widget cmdsScrollFieldReporter with the following code we should get ;
Working color syntaxer class | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
from PySide.QtGui import * from PySide.QtCore import * from shiboken import wrapInstance from maya.OpenMayaUI import MQtUtil class StdOut_Syntax(QSyntaxHighlighter): def __init__(self, parent): super(StdOut_Syntax, self).__init__(parent) self.parent = parent def highlightBlock(self, text): color = QColor(255, 125, 160) pattern = QRegExp(r'^//.+$') # regexp pattern keyword = QTextCharFormat() keyword.setForeground(color) # defining the aspect index = pattern.indexIn(text) while index >= 0: # loop through the text to find matches len = pattern.matchedLength() # length of the match self.setFormat(index, len, keyword) # we apply the format to the match index = pattern.indexIn(text, index + len) self.setCurrentBlockState(0) def wrap(): i = 1 while i: try: se_edit = wrapInstance(long(MQtUtil.findControl('cmdScrollFieldReporter%i' % i)), QTextEdit) # we remove the old syntax and raise an exception to get out of the while assert se_edit.findChild(QSyntaxHighlighter).deleteLater() except TypeError: i += 1 # if we don't find the widget we increment ' except (AttributeError, AssertionError): break return StdOut_Syntax(se_edit) wrap() |
This means each time we'll meet the regular expression of "a line starting with // until the end of the line" will get a red color ! Thus ;

We see the light =)
Now let's have a look how to "link" our QSyntaxHighlighter to Maya !
Author: mlouala
Submitted: 2017-01-07 07:22:24 UTC
Tags: Color, script editor, syntax, script, and syntax highlighting
Software: Maya
Views: 5,771
Related Items
-
Universal UV Randomization script for Maya 1.1.0 (maya script)
$20.00 (USD) -
Render Window / viewport Batch Render Script for Maya 1.0.0 (maya script)
$20.00 (USD) -
UV Texture Editor Tools Ur-edition for Maya 2.1.2 (maya script)
$39.00 (USD) -
Nightshade UV Editor Pro for Maya 2.1.3 (maya script)
$39.90 (USD) -
Christmas Color Ball 3D Model
$20.00 (USD) -
3D Thumbnail Generator (batch script) for Maya 0.3.1 (maya script)
$20.00 (USD) -
Attr Editor for Maya 1.3.0 (maya script)
$25.00 (USD) -
Color T-Shirt 3D Model
$38.00 (USD) -
Samsung Galaxy S6 All Color Pack 3D Model
$49.99 (USD)