You have to install a filterObj
to your QTextEdit
widget (Doc: QObject::installEventFilter()) inside the __init__()
builder of your `MainWindow.
Now, your code will work fine, except for one thing: when you write more words in the second TextEdit
, the program will no recognize the error.
Doing some "improvements" to your code (Just to test, if you cando it better, perfect) your __init__()
will look like this:
def __init__(self):
...# some code to build your app
self.textEdit_2.installEventFilter(self) ## <<---HERE IS THE CRUCIAL PART
self.good = True
, your eventFilter()
will look like this:
def eventFilter(self, obj, event):
if obj is self.textEdit_2 and event.type() == QtCore.QEvent.KeyPress:
if event.key() == QtCore.Qt.Key_Space:
self.check()
elif event.key() == QtCore.Qt.Key_Backspace:
self.textEdit_2.clear()
self.good = True
return True
else:
if not self.good:
var2 = self.textEdit_2.toPlainText().split()
sw = var2.pop()
self.textEdit_2.clear()
self.textEdit_2.append(' '.join(var2)+' '+f"<span style='background-color: yellow;'>{sw}</p>")
self.good = False
return super(MainWindow, self).eventFilter(obj, event)
And, finally, your check()
will look like this:
def check(self):
var = self.textEdit.toPlainText().split()
var2 = self.textEdit_2.toPlainText().split()
j = len(var)-1
i = len(var2)-1
if i-j <= 0:
if var[i] != var2[i]:
sw = var2.pop()
self.textEdit_2.clear()
self.textEdit_2.append(' '.join(var2)+' '+f"<span style='background-color: yellow;'>{sw}</p>")
self.good = False
elif i-j == 1:
sw = var2.pop()
self.textEdit_2.clear()
self.textEdit_2.append(' '.join(var2)+' '+f"<span style='background-color: yellow;'>{sw}</p>")
self.good = False
The self.good
is just a bool variable that I made to let your App know if the current text has errors or not. Here is how it look when I run a simple App I did to test if it works:
Edit
To find all the errors in the second QTextEdit
Now, you commented that you need to find all the errors in textEdit_2
. So I did these:
- Delete the
self.good
bool variable I made before.
- Change a bit the
eventFilter()
function
- Rewrite your
check()
function.
The code is here:
def __init__(self):
...# some code to build your app
self.textEdit_2.installEventFilter(self)
def eventFilter(self, obj, event):
if obj is self.textEdit_2 and event.type() == QtCore.QEvent.KeyPress:
if event.key() == QtCore.Qt.Key_Space:
self.check()
elif event.key() == QtCore.Qt.Key_Backspace:
self.textEdit_2.clear()
return True
return super(MainWindow, self).eventFilter(obj, event)
def check(self):
var_1 = self.textEdit.toPlainText().split()
var_2 = self.textEdit_2.toPlainText().split()
len_1 = len(var_1)
len_2 = len(var_2)
self.textEdit_2.clear()
text = []
for it in range(len_2):
if it < len_1:
if var_1[it] == var_2[it]:
text += [var_2[it]]
else:
text += ["<span style='background-color: yellow;'>"+var_2[it]+"</span>"]
else:
text += ["<span style='background-color: yellow;'>"+var_2[it]+"</span>"]
text = ' '.join(text)
self.textEdit_2.append(text)
And It looks like this:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…