Reputation: 11
Trying to use the builtin 'find' function of the QTextEdit widget but when I try to pass the text from a QLineEdit widget, it gives me the following error:
Traceback (most recent call last):
File "C:\SVN\Gocator\Trunk\Gocator\GoPy\Scripts\testfind.pyw", line 52, in on_find_button_clicked
self.fileEdit.find(self.findLine.text)
TypeError: QTextEdit.find(str, QTextDocument.FindFlags options=0): argument 1 has unexpected type 'builtin_function_or_method'
I've reviewed the documentation for the QTextEdit class and there's not much to it but I can't figure out why it's giving me the error. What's interesting is if I replace the reference to the QLineEdit text property with a string literal (e.g. "What") in the find() call (line 52: self.fileEdit.find(self.findLine.text)), it will work.
My test code is pretty straight forward so I think it's just something I'm not seeing right in front of my eyes. Does anyone see where I went wrong or even get the same issue? Here's my test script (I only have Qt4 installed):
#!/usr/bin/env python
# Needs Qt5 (recommended) or Qt4
# PyQt5: run pip3 install pyqt5
# PyQt4: from http://www.riverbankcomputing.com/software/pyqt/download
import shelve
import sys
sys.path.append('../GoPy')
sys.path.append('../../GoPy')
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class ControlEngine:
def __init__(self):
self.shelf = shelve.open("ui_control.shelf")
class MainWindow(QWidget):
def __init__(self):
QWidget.__init__(self)
self.engine = ControlEngine()
self.create_ui()
def create_ui(self):
mainLayout = QVBoxLayout()
# View group
viewGroup = QGroupBox("File View")
viewLayout = QVBoxLayout()
self.fileEdit = QTextEdit("Today at Safeway, here's what we have for you.")
self.fileEdit.setFont(QFont("Courier New", 10))
viewLayout.addWidget(self.fileEdit)
self.findButton = QPushButton("Find Next Word")
self.findButton.clicked.connect(self.on_find_button_clicked)
self.findLine = QLineEdit("What")
viewLayout.addWidget(self.findLine)
viewLayout.addWidget(self.findButton)
viewGroup.setLayout(viewLayout)
mainLayout.addWidget(viewGroup)
self.setLayout(mainLayout)
self.fileEdit.moveCursor(1)
@pyqtSlot(int, int)
def on_find_button_clicked(self):
self.fileEdit.find(self.findLine.text)
class App(QApplication):
def __init__(self, argv):
QApplication.__init__(self, argv)
def start(self):
mainWindow = MainWindow()
self.mainWindow = mainWindow
mainWindow.resize(300, 300)
mainWindow.setWindowTitle("Test Find")
mainWindow.show()
return self.exec_()
def main():
app = App(sys.argv)
sys.exit(app.start())
if __name__ == '__main__':
main()
Upvotes: 1
Views: 1194
Reputation: 154
Change this
self.fileEdit.find(self.findLine.text)
to
self.fileEdit.find(self.findLine.text())
because its a function not a property :)
Upvotes: 1