diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2020-07-03 18:12:01 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2020-07-06 22:07:21 +0200 |
commit | 735d8097489f836f32f65b8ff47f3e904642faf4 (patch) | |
tree | 42a5ae3ba757eb0b8686f0c3f98b5123f45d3b57 /examples | |
parent | fa1a682cfdc0238a7ca10ffd45ea5f6ada4c8e7e (diff) |
Port some examples from QRegExp to QRegularExpression
QRegExp is deprecated.
Task-number: PYSIDE-1339
Task-number: PYSIDE-904
Change-Id: I5c5dc4965a03fbd1a3370be3fa9e64c5df6a9fd8
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
Diffstat (limited to 'examples')
-rw-r--r-- | examples/corelib/tools/codecs/codecs.py | 17 | ||||
-rw-r--r-- | examples/opengl/grabber.py | 10 | ||||
-rw-r--r-- | examples/widgets/dialogs/classwizard/classwizard.py | 2 | ||||
-rw-r--r-- | examples/widgets/itemviews/addressbook/addresswidget.py | 9 | ||||
-rw-r--r-- | examples/widgets/richtext/syntaxhighlighter.py | 56 | ||||
-rw-r--r-- | examples/xmlpatterns/schema/schema.py | 43 |
6 files changed, 80 insertions, 57 deletions
diff --git a/examples/corelib/tools/codecs/codecs.py b/examples/corelib/tools/codecs/codecs.py index a3c063c04..63e74a60b 100644 --- a/examples/corelib/tools/codecs/codecs.py +++ b/examples/corelib/tools/codecs/codecs.py @@ -123,7 +123,8 @@ class MainWindow(QtWidgets.QMainWindow): def findCodecs(self): codecMap = [] - iso8859RegExp = QtCore.QRegExp('ISO[- ]8859-([0-9]+).*') + iso8859RegExp = QtCore.QRegularExpression('^ISO[- ]8859-([0-9]+).*$') + assert iso8859RegExp.isValid() for mib in QtCore.QTextCodec.availableMibs(): codec = QtCore.QTextCodec.codecForMib(mib) @@ -134,13 +135,15 @@ class MainWindow(QtWidgets.QMainWindow): rank = 1 elif sortKey.startswith('UTF-16'): rank = 2 - elif iso8859RegExp.exactMatch(sortKey): - if len(iso8859RegExp.cap(1)) == 1: - rank = 3 - else: - rank = 4 else: - rank = 5 + match = iso8859RegExp.match(sortKey) + if match.hasMatch(): + if len(match.captured(1)) == 1: + rank = 3 + else: + rank = 4 + else: + rank = 5 codecMap.append((str(rank) + sortKey, codec)) diff --git a/examples/opengl/grabber.py b/examples/opengl/grabber.py index d4b625718..4c8f9a0d2 100644 --- a/examples/opengl/grabber.py +++ b/examples/opengl/grabber.py @@ -414,11 +414,13 @@ class MainWindow(QtWidgets.QMainWindow): if not ok: return QtCore.QSize() - regExp = QtCore.QRegExp("([0-9]+) *x *([0-9]+)") + regExp = QtCore.QRegularExpression("([0-9]+) *x *([0-9]+)") + assert regExp.isValid() - if regExp.exactMatch(text): - width = int(regExp.cap(1)) - height = int(regExp.cap(2)) + match = regExp.match(text) + if match.hasMatch(): + width = int(match.captured(1)) + height = int(match.captured(2)) if width > 0 and width < 2048 and height > 0 and height < 2048: return QtCore.QSize(width, height) diff --git a/examples/widgets/dialogs/classwizard/classwizard.py b/examples/widgets/dialogs/classwizard/classwizard.py index fc2feb498..d0e970f94 100644 --- a/examples/widgets/dialogs/classwizard/classwizard.py +++ b/examples/widgets/dialogs/classwizard/classwizard.py @@ -325,7 +325,7 @@ class CodeStylePage(QtWidgets.QWizardPage): if not is_baseClass: self.baseIncludeLineEdit.clear() - elif QtCore.QRegExp('Q[A-Z].*').exactMatch(baseClass): + elif QtCore.QRegularExpression('^Q[A-Z].*$').match(baseClass).hasMatch(): self.baseIncludeLineEdit.setText('<' + baseClass + '>') else: self.baseIncludeLineEdit.setText('"' + baseClass.lower() + '.h"') diff --git a/examples/widgets/itemviews/addressbook/addresswidget.py b/examples/widgets/itemviews/addressbook/addresswidget.py index b70b44b0a..d0c1747fb 100644 --- a/examples/widgets/itemviews/addressbook/addresswidget.py +++ b/examples/widgets/itemviews/addressbook/addresswidget.py @@ -45,7 +45,7 @@ try: except ImportError: import pickle -from PySide2.QtCore import (Qt, Signal, QRegExp, QModelIndex, +from PySide2.QtCore import (Qt, Signal, QRegularExpression, QModelIndex, QItemSelection, QSortFilterProxyModel) from PySide2.QtWidgets import QTabWidget, QMessageBox, QTableView, QAbstractItemView @@ -193,9 +193,10 @@ class AddressWidget(QTabWidget): # tab. The regex will end up looking like "^[ABC].*", only # allowing this tab to display items where the name starts with # "A", "B", or "C". Notice that we set it to be case-insensitive. - reFilter = "^[%s].*" % group - - proxyModel.setFilterRegExp(QRegExp(reFilter, Qt.CaseInsensitive)) + re = QRegularExpression("^[{}].*".format(group)) + assert re.isValid() + re.setPatternOptions(QRegularExpression.CaseInsensitiveOption) + proxyModel.setFilterRegularExpression(re) proxyModel.setFilterKeyColumn(0) # Filter on the "name" column proxyModel.sort(0, Qt.AscendingOrder) diff --git a/examples/widgets/richtext/syntaxhighlighter.py b/examples/widgets/richtext/syntaxhighlighter.py index 82e4c79a1..9be299401 100644 --- a/examples/widgets/richtext/syntaxhighlighter.py +++ b/examples/widgets/richtext/syntaxhighlighter.py @@ -130,65 +130,73 @@ class Highlighter(QtGui.QSyntaxHighlighter): "\\bunion\\b", "\\bunsigned\\b", "\\bvirtual\\b", "\\bvoid\\b", "\\bvolatile\\b"] - self.highlightingRules = [(QtCore.QRegExp(pattern), keywordFormat) + self.highlightingRules = [(QtCore.QRegularExpression(pattern), keywordFormat) for pattern in keywordPatterns] classFormat = QtGui.QTextCharFormat() classFormat.setFontWeight(QtGui.QFont.Bold) classFormat.setForeground(QtCore.Qt.darkMagenta) - self.highlightingRules.append((QtCore.QRegExp("\\bQ[A-Za-z]+\\b"), - classFormat)) + pattern = QtCore.QRegularExpression(r'\bQ[A-Za-z]+\b') + assert pattern.isValid() + self.highlightingRules.append((pattern, classFormat)) singleLineCommentFormat = QtGui.QTextCharFormat() singleLineCommentFormat.setForeground(QtCore.Qt.red) - self.highlightingRules.append((QtCore.QRegExp("//[^\n]*"), - singleLineCommentFormat)) + pattern = QtCore.QRegularExpression('//[^\n]*') + assert pattern.isValid() + self.highlightingRules.append((pattern, singleLineCommentFormat)) self.multiLineCommentFormat = QtGui.QTextCharFormat() self.multiLineCommentFormat.setForeground(QtCore.Qt.red) quotationFormat = QtGui.QTextCharFormat() quotationFormat.setForeground(QtCore.Qt.darkGreen) - self.highlightingRules.append((QtCore.QRegExp("\".*\""), - quotationFormat)) + pattern = QtCore.QRegularExpression('".*"') + assert pattern.isValid() + self.highlightingRules.append((pattern, quotationFormat)) functionFormat = QtGui.QTextCharFormat() functionFormat.setFontItalic(True) functionFormat.setForeground(QtCore.Qt.blue) - self.highlightingRules.append((QtCore.QRegExp("\\b[A-Za-z0-9_]+(?=\\()"), - functionFormat)) + pattern = QtCore.QRegularExpression(r'\b[A-Za-z0-9_]+(?=\()') + assert pattern.isValid() + self.highlightingRules.append((pattern, functionFormat)) - self.commentStartExpression = QtCore.QRegExp("/\\*") - self.commentEndExpression = QtCore.QRegExp("\\*/") + self.commentStartExpression = QtCore.QRegularExpression(r'/\*') + assert self.commentStartExpression.isValid() + self.commentEndExpression = QtCore.QRegularExpression(r'\*/') + assert self.commentEndExpression.isValid() def highlightBlock(self, text): for pattern, format in self.highlightingRules: - expression = QtCore.QRegExp(pattern) - index = expression.indexIn(text) - while index >= 0: - length = expression.matchedLength() + match = pattern.match(text) + while match.hasMatch(): + index = match.capturedStart(0) + length = match.capturedLength(0) self.setFormat(index, length, format) - index = expression.indexIn(text, index + length) + match = pattern.match(text, index + length) self.setCurrentBlockState(0) startIndex = 0 if self.previousBlockState() != 1: - startIndex = self.commentStartExpression.indexIn(text) + match = self.commentStartExpression.match(text) + startIndex = match.capturedStart(0) if match.hasMatch() else -1 while startIndex >= 0: - endIndex = self.commentEndExpression.indexIn(text, startIndex) - - if endIndex == -1: + match = self.commentEndExpression.match(text, startIndex) + if match.hasMatch(): + endIndex = match.capturedStart(0) + length = match.capturedLength(0) + commentLength = endIndex - startIndex + length + else: self.setCurrentBlockState(1) commentLength = len(text) - startIndex - else: - commentLength = endIndex - startIndex + self.commentEndExpression.matchedLength() self.setFormat(startIndex, commentLength, self.multiLineCommentFormat) - startIndex = self.commentStartExpression.indexIn(text, - startIndex + commentLength) + match = self.commentStartExpression.match(text, startIndex + commentLength) + startIndex = match.capturedStart(0) if match.hasMatch() else -1 if __name__ == '__main__': diff --git a/examples/xmlpatterns/schema/schema.py b/examples/xmlpatterns/schema/schema.py index 677b56498..d3c22c184 100644 --- a/examples/xmlpatterns/schema/schema.py +++ b/examples/xmlpatterns/schema/schema.py @@ -77,19 +77,22 @@ class XmlSyntaxHighlighter(QtGui.QSyntaxHighlighter): format = QtGui.QTextCharFormat() format.setForeground(QtCore.Qt.darkBlue) format.setFontWeight(QtGui.QFont.Bold) - pattern = QtCore.QRegExp("(<[a-zA-Z:]+\\b|<\\?[a-zA-Z:]+\\b|\\?>|>|/>|</[a-zA-Z:]+>)") + pattern = QtCore.QRegularExpression(r'(<[a-zA-Z:]+\b|<\?[a-zA-Z:]+\b|\?>|>|/>|</[a-zA-Z:]+>)') + assert pattern.isValid() self.highlightingRules.append((pattern, format)) # Attribute format. format = QtGui.QTextCharFormat() format.setForeground(QtCore.Qt.darkGreen) - pattern = QtCore.QRegExp("[a-zA-Z:]+=") + pattern = QtCore.QRegularExpression('[a-zA-Z:]+=') + assert pattern.isValid() self.highlightingRules.append((pattern, format)) # Attribute content format. format = QtGui.QTextCharFormat() format.setForeground(QtCore.Qt.red) - pattern = QtCore.QRegExp("(\"[^\"]*\"|'[^']*')") + pattern = QtCore.QRegularExpression("(\"[^\"]*\"|'[^']*')") + assert pattern.isValid() self.highlightingRules.append((pattern, format)) # Comment format. @@ -97,35 +100,41 @@ class XmlSyntaxHighlighter(QtGui.QSyntaxHighlighter): self.commentFormat.setForeground(QtCore.Qt.lightGray) self.commentFormat.setFontItalic(True) - self.commentStartExpression = QtCore.QRegExp("<!--") - self.commentEndExpression = QtCore.QRegExp("-->") + self.commentStartExpression = QtCore.QRegularExpression("<!--") + assert self.commentStartExpression.isValid() + self.commentEndExpression = QtCore.QRegularExpression("-->") + assert self.commentEndExpression.isValid() def highlightBlock(self, text): for pattern, format in self.highlightingRules: - expression = QtCore.QRegExp(pattern) - index = expression.indexIn(text) - while index >= 0: - length = expression.matchedLength() + match = pattern.match(text) + while match.hasMatch(): + index = match.capturedStart() + length = match.capturedLength(0) self.setFormat(index, length, format) - index = expression.indexIn(text, index + length) + match = pattern.match(text, index + length) self.setCurrentBlockState(0) startIndex = 0 if self.previousBlockState() != 1: - startIndex = self.commentStartExpression.indexIn(text) + match = self.commentStartExpression.match(text) + startIndex = match.capturedStart(0) if match.hasMatch() else -1 while startIndex >= 0: - endIndex = self.commentEndExpression.indexIn(text, startIndex) - if endIndex == -1: + match = self.commentEndExpression.match(text, startIndex) + endIndex = match.capturedStart(0) if match.hasMatch() else -1 + if match.hasMatch(): + endIndex = match.capturedStart(0) + length = match.capturedLength(0) + commentLength = endIndex - startIndex + length + else: self.setCurrentBlockState(1) commentLength = text.length() - startIndex - else: - commentLength = endIndex - startIndex + self.commentEndExpression.matchedLength() self.setFormat(startIndex, commentLength, self.commentFormat) - startIndex = self.commentStartExpression.indexIn(text, - startIndex + commentLength) + match = self.commentStartExpression.match(text, startIndex + commentLength) + startIndex = match.capturedStart(0) if match.hasMatch() else -1 class MessageHandler(QtXmlPatterns.QAbstractMessageHandler): |