本文整理汇总了Python中PySide.QtCore.QRegExp.pos方法的典型用法代码示例。如果您正苦于以下问题:Python QRegExp.pos方法的具体用法?Python QRegExp.pos怎么用?Python QRegExp.pos使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PySide.QtCore.QRegExp
的用法示例。
在下文中一共展示了QRegExp.pos方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: MacroHighlighter
# 需要导入模块: from PySide.QtCore import QRegExp [as 别名]
# 或者: from PySide.QtCore.QRegExp import pos [as 别名]
class MacroHighlighter(QSyntaxHighlighter):
def __init__(self,textboxdoc,valid_list_of_commands):
QSyntaxHighlighter.__init__(self,textboxdoc)
self.valid_syntax="|".join([command.regexp_str for command in valid_list_of_commands])
self.my_expression = QRegExp(self.valid_syntax)
#define a blue font format for valid commands
self.valid = QTextCharFormat()
self.valid.setForeground(Qt.black)
#define a bold red font format for invalid commands
self.invalid = QTextCharFormat()
self.invalid.setFontWeight(QFont.Bold)
self.invalid.setForeground(Qt.red)
#define a blue font format for valid parameters
self.valid_value=QTextCharFormat()
self.valid_value.setFontWeight(QFont.Bold)
#self.valid_value.setForeground(QColor.fromRgb(255,85,0))
self.valid_value.setForeground(Qt.blue)
def highlightBlock(self, text):
#this function is automatically called when some text is changed
#in the texbox. 'text' is the line of text where the change occured
#check if the line of text contains a valid command
match = self.my_expression.exactMatch(text)
if match:
#valid command found: highlight the command in blue
self.setFormat(0, len(text), self.valid)
#highlight the parameters in orange
#loop on all the parameters that can be captured
for i in range(self.my_expression.captureCount()):
#if a parameter was captured, it's position in the text will be >=0 and its capture contains some value 'xxx'
#otherwise its position is -1 and its capture contains an empty string ''
if self.my_expression.pos(i+1)!=-1:
self.setFormat(self.my_expression.pos(i+1), len(self.my_expression.cap(i+1)), self.valid_value)
else:
#no valid command found: highlight in red
self.setFormat(0, len(text), self.invalid)
示例2: highlightBlock
# 需要导入模块: from PySide.QtCore import QRegExp [as 别名]
# 或者: from PySide.QtCore.QRegExp import pos [as 别名]
def highlightBlock(self, text):
""" Highlight a block of text """
if self.enabled is False:
return
text = unicode(text)
original_text = text
prev_data = self.currentBlock().previous().userData()
if prev_data is not None:
self._lexer._saved_state_stack = prev_data.syntax_stack
elif hasattr(self._lexer, '_saved_state_stack'):
del self._lexer._saved_state_stack
# Lex the text using Pygments
index = 0
for token, text in self._lexer.get_tokens(text):
length = len(text)
self.setFormat(index, length, self._get_format(token))
index += length
if hasattr(self._lexer, '_saved_state_stack'):
data = PygmentsBlockUserData(
syntax_stack=self._lexer._saved_state_stack)
self.currentBlock().setUserData(data)
# Clean up for the next go-round.
del self._lexer._saved_state_stack
#Spaces
expression = QRegExp('\s+')
index = expression.indexIn(original_text, 0)
while index >= 0:
index = expression.pos(0)
length = len(expression.cap(0))
self.setFormat(index, length, self._get_format(Whitespace))
index = expression.indexIn(original_text, index + length)
self.hilighlightingBlock.emit(original_text, self)