I am building a ASCIIValidator with the help of the Qt c++ source code which relies on the QRegularExpression classes instead of their old QRegExp counterparts. To make an example, please take a look at line 916 of https://github.com/qt/qtbase/blob/dev/src/gui/util/qvalidator.cpp
class ASCIIValidator(QtGui.QRegularExpressionValidator):
def __init__(self):
super(ASCIIValidator, self).__init__()
self.description = ""
self.invalidInputMsg = ""
self.regex = QtCore.QRegularExpression(r"[^\x00-\x7F]")
self.setRegularExpression(self.regex)
self.regex.setCaseSensitivity(QtCore.Qt.CaseInsensitive)
def validate(self, _input: str, pos: int) -> QtGui.QValidator.State:
if self.regularExpression().pattern().isEmpty():
return QtGui.QValidator.Acceptable
match = self.regularExpression().match(_input, 0, QtCore.QRegularExpression.PartialPreferCompleteMatch)
if match.hasMatch(): # means that a non-ascii character has been foound
return QtGui.QValidator.Invalid
else:
return QtGui.QValidator.Acceptable
throws the following error:
from util.UValidator import UValidator
File ".....\UValidator.py", line 38, in <module>
class ASCIIValidator(QtGui.QRegularExpressionValidator):
AttributeError: module 'PySide2.QtGui' has no attribute 'QRegularExpressionValidator'
However, the Qtdocs as well as PySide2 docs list QRegularExpressionValidator as part of QtGui and even recommend using them over the old QRegExp classes. I'm using lastest python3 with pyside2 installed via pip. EDIT: using PySide 5.15