【发布时间】:2017-04-22 21:16:14
【问题描述】:
尝试使用 QTextEdit 小部件的内置“查找”功能,但当我尝试从 QLineEdit 小部件传递文本时,出现以下错误:
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'
我查看了 QTextEdit 类的文档,并没有太多内容,但我不知道为什么它给了我错误。有趣的是,如果我在 find() 调用中用字符串文字(例如“What”)替换对 QLineEdit 文本属性的引用(第 52 行:self.fileEdit.find(self.findLine.text)),它会工作。
我的测试代码非常简单,所以我认为这只是我眼前没有看到的东西。有没有人看到我哪里出错了,甚至遇到了同样的问题?这是我的测试脚本(我只安装了 Qt4):
#!/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()
【问题讨论】: