def __init__(self, parent=None):
super(Highlighter, self).__init__(parent)
keywordFormat = QTextCharFormat()
keywordFormat.setForeground(Qt.darkBlue)
keywordFormat.setFontWeight(QFont.Bold)
keywordPatterns = ["\\bchar\\b", "\\bclass\\b", "\\bconst\\b",
"\\bdouble\\b", "\\benum\\b", "\\bexplicit\\b", "\\bfriend\\b",
"\\binline\\b", "\\bint\\b", "\\blong\\b", "\\bnamespace\\b",
"\\boperator\\b", "\\bprivate\\b", "\\bprotected\\b",
"\\bpublic\\b", "\\bshort\\b", "\\bsignals\\b", "\\bsigned\\b",
"\\bslots\\b", "\\bstatic\\b", "\\bstruct\\b",
"\\btemplate\\b", "\\btypedef\\b", "\\btypename\\b",
"\\bunion\\b", "\\bunsigned\\b", "\\bvirtual\\b", "\\bvoid\\b",
"\\bvolatile\\b"]
self.highlightingRules = [(QRegExp(pattern), keywordFormat)
for pattern in keywordPatterns]
classFormat = QTextCharFormat()
classFormat.setFontWeight(QFont.Bold)
classFormat.setForeground(Qt.darkMagenta)
self.highlightingRules.append((QRegExp("\\bQ[A-Za-z]+\\b"),
classFormat))
singleLineCommentFormat = QTextCharFormat()
singleLineCommentFormat.setForeground(Qt.red)
self.highlightingRules.append((QRegExp("//[^\n]*"),
singleLineCommentFormat))
self.multiLineCommentFormat = QTextCharFormat()
self.multiLineCommentFormat.setForeground(Qt.red)
quotationFormat = QTextCharFormat()
quotationFormat.setForeground(Qt.darkGreen)
self.highlightingRules.append((QRegExp("\".*\""), quotationFormat))
functionFormat = QTextCharFormat()
functionFormat.setFontItalic(True)
functionFormat.setForeground(Qt.blue)
self.highlightingRules.append((QRegExp("\\b[A-Za-z0-9_]+(?=\\()"),
functionFormat))
self.commentStartExpression = QRegExp("/\\*")
self.commentEndExpression = QRegExp("\\*/")
python类blue()的实例源码
def createTextFormatsGroupBox(self):
self.textFormatsGroupBox = QGroupBox("Text Formats")
self.weekdayColorCombo = self.createColorComboBox()
self.weekdayColorCombo.setCurrentIndex(
self.weekdayColorCombo.findText("Black"))
self.weekdayColorLabel = QLabel("&Weekday color:")
self.weekdayColorLabel.setBuddy(self.weekdayColorCombo)
self.weekendColorCombo = self.createColorComboBox()
self.weekendColorCombo.setCurrentIndex(
self.weekendColorCombo.findText("Red"))
self.weekendColorLabel = QLabel("Week&end color:")
self.weekendColorLabel.setBuddy(self.weekendColorCombo)
self.headerTextFormatCombo = QComboBox()
self.headerTextFormatCombo.addItem("Bold")
self.headerTextFormatCombo.addItem("Italic")
self.headerTextFormatCombo.addItem("Plain")
self.headerTextFormatLabel = QLabel("&Header text:")
self.headerTextFormatLabel.setBuddy(self.headerTextFormatCombo)
self.firstFridayCheckBox = QCheckBox("&First Friday in blue")
self.mayFirstCheckBox = QCheckBox("May &1 in red")
self.weekdayColorCombo.currentIndexChanged.connect(
self.weekdayFormatChanged)
self.weekendColorCombo.currentIndexChanged.connect(
self.weekendFormatChanged)
self.headerTextFormatCombo.currentIndexChanged.connect(
self.reformatHeaders)
self.firstFridayCheckBox.toggled.connect(self.reformatCalendarPage)
self.mayFirstCheckBox.toggled.connect(self.reformatCalendarPage)
checkBoxLayout = QHBoxLayout()
checkBoxLayout.addWidget(self.firstFridayCheckBox)
checkBoxLayout.addStretch()
checkBoxLayout.addWidget(self.mayFirstCheckBox)
outerLayout = QGridLayout()
outerLayout.addWidget(self.weekdayColorLabel, 0, 0)
outerLayout.addWidget(self.weekdayColorCombo, 0, 1)
outerLayout.addWidget(self.weekendColorLabel, 1, 0)
outerLayout.addWidget(self.weekendColorCombo, 1, 1)
outerLayout.addWidget(self.headerTextFormatLabel, 2, 0)
outerLayout.addWidget(self.headerTextFormatCombo, 2, 1)
outerLayout.addLayout(checkBoxLayout, 3, 0, 1, 2)
self.textFormatsGroupBox.setLayout(outerLayout)
self.weekdayFormatChanged()
self.weekendFormatChanged()
self.reformatHeaders()
self.reformatCalendarPage()