aboutsummaryrefslogtreecommitdiffstats
path: root/examples/widgets
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2020-07-03 18:12:01 +0200
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2020-07-06 22:07:21 +0200
commit735d8097489f836f32f65b8ff47f3e904642faf4 (patch)
tree42a5ae3ba757eb0b8686f0c3f98b5123f45d3b57 /examples/widgets
parentfa1a682cfdc0238a7ca10ffd45ea5f6ada4c8e7e (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/widgets')
-rw-r--r--examples/widgets/dialogs/classwizard/classwizard.py2
-rw-r--r--examples/widgets/itemviews/addressbook/addresswidget.py9
-rw-r--r--examples/widgets/richtext/syntaxhighlighter.py56
3 files changed, 38 insertions, 29 deletions
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__':