Simple code demonstrating the problem:
#!/usr/bin/env python
import sys
from PyQt4.QtCore import QObject, SIGNAL
from PyQt4.QtGui import QApplication, QTextEdit
app = QApplication(sys.argv)
def findText():
print(textEdit.find('A'))
textEdit = QTextEdit()
textEdit.show()
QObject.connect(textEdit, SIGNAL('textChanged()'), findText)
sys.exit(app.exec_())
After typing ‘A’ into the window, find('A') still returns False.
Where is the problem?
The problem is the position of the cursor in the window.
By default – unless you specify some flags to be passed to the
find()function, the search only happens forward (= from the position of the cursor onwards).In order to make your test work, you should do something like this:
BACThis way you will have in the window the string
CBA, with the cursor betweenCandBand the string onto which thefind()method will work returningTruewill beBA.Alternatively you can test this other version of your code that has the backward flag set.