QTextDocument find returns wrong cursor position
-
I am trying to make a search dialog for my application, right now I am working on the find function. So far it works, but it only finds the first match in the text area and it won't search past that:
@
Find: a
b
b
a <--- When ever I hit 'find' it goes here no matter where the text cursor is
b
b
a <--- it won't go here unless I get rid of the first 'a'@Here is my code:
@
#!/usr/bin/pythonimport sys
from PyQt4 import QtGui, QtCoreclass Example(QtGui.QWidget):
def __init__(self): super(Example, self).__init__() self.initUI() def initUI(self): button = QtGui.QPushButton('Find', self) self.lineEdit = QtGui.QLineEdit(self) self.textEdit = QtGui.QTextEdit(self) button.move(1, 200) self.lineEdit.move(100, 203) button.clicked.connect(self.TextSearch) self.setGeometry(300, 300, 260, 230) self.setWindowTitle('QtGui.QLineEdit') self.show() def TextSearch(self): searchString = self.lineEdit.text(); # text to look for document = self.textEdit.document(); # File if searchString.isEmpty(): print "Nothing to search for!" return c = QtGui.QTextCursor(document); while not c.isNull() and not c.atEnd(): c = document.find(searchString, c, QtGui.QTextDocument.FindWholeWords); if not c.isNull(): self.textEdit.setTextCursor(c); self.textEdit.setFocus() return print "Word not found."
def main():
app = QtGui.QApplication(sys.argv) ex = Example() sys.exit(app.exec_())
if name == 'main':
main()@Look at TextSearch for the part where I search for the text.