Python 发出新信号时的即时文本搜索/中断

Python 发出新信号时的即时文本搜索/中断,python,qt,pyqt,signals,Python,Qt,Pyqt,Signals,我使用QLineEdit中的textChanged信号在PyQt4中进行即时搜索 搜索速度足够快,但是用结果更新QTextEdit有点慢(或者我编写的函数,它构建了输出字符串)无论如何,如果在一定时间内释放新的textChanged信号,是否有办法稍微延迟搜索或中断搜索?目标是避免在以普通键入速度向搜索字段键入内容时输入“挂起” 或者其他解决这个问题的建议?也许不穿线 基本上是这样的 class MyApp(..): def __init__(self): ....

我使用
QLineEdit
中的
textChanged
信号在PyQt4中进行即时搜索

搜索速度足够快,但是用结果更新
QTextEdit
有点慢(或者我编写的函数,它构建了输出字符串)无论如何,如果在一定时间内释放新的
textChanged
信号,是否有办法稍微延迟搜索或中断搜索?
目标是避免在以普通键入速度向搜索字段键入内容时输入“挂起”

或者其他解决这个问题的建议?也许不穿线

基本上是这样的

class MyApp(..):

    def __init__(self):
        ....

        self.central.editSearch.textChanged.connect(self.search_instant)

    def search_instant(self, query):
        # skip strings with less than 3 chars
        if len(query) < 3:
            return
        self.search()

    def search(self)
        ... search in lots of strings ...
        self.central.myTextEdit.setText(result)
        # reimplemented function building the output
类MyApp(…):
定义初始化(自):
....
self.central.editSearch.textChanged.connect(self.search\u即时)
def搜索_即时(自我,查询):
#跳过少于3个字符的字符串
如果len(查询)<3:
返回
self.search()
def搜索(自我)
... 搜索大量字符串。。。
self.central.myTextEdit.setText(结果)
#重新实现的函数生成输出
peakxu建议说,这是我从谷歌的例子中得出的结论

class MyApp(..):

    def __init__(self):
        ....

        self.central.editSearch.textChanged.connect(self.search_instant)

        # Timer for instant search
        # Signal is emitted 500 ms after timer was started
        self.timer = QTimer()
        self.timer.setSingleShot(True)
        self.timer.setInterval(500)
        self.timer.timeout.connect(self.search)


    def search_instant(self, query):
        # Stop timer (if its running)
        self.timer.stop()

        # skip strings with less than 3 chars
        if len(query) < 3:
            return

        # Start the timer
        self.timer.start()    

    def search(self)
        # The search will only performed if typing paused for at least 500 ms
        # i. e. no `textChanged` signal was emitted for 500 ms
        ... search in lots of strings ...
        self.central.myTextEdit.setText(result)
        # reimplemented function building the output
类MyApp(…):
定义初始化(自):
....
self.central.editSearch.textChanged.connect(self.search\u即时)
#即时搜索计时器
#定时器启动500毫秒后发出信号
self.timer=QTimer()
self.timer.setSingleShot(真)
自动定时器设置间隔(500)
self.timer.timeout.connect(self.search)
def搜索_即时(自我,查询):
#停止计时器(如果正在运行)
self.timer.stop()
#跳过少于3个字符的字符串
如果len(查询)<3:
返回
#启动计时器
self.timer.start()
def搜索(自我)
#仅当键入暂停至少500毫秒时,才会执行搜索
#一,。E500毫秒内未发出“textChanged”信号
... 搜索大量字符串。。。
self.central.myTextEdit.setText(结果)
#重新实现的函数生成输出

> > P>有几种选择,你可能需要考虑。< /P>
  • 让搜索在线程中运行,必要时中断。看
  • 阻止信号,直到之前的搜索完成
  • 围绕搜索函数编写自己的去盎司包装器
  • 第一种选择目前听起来最好

    更新:
    这可能是一个有用的参考。他们使用QTimer来跟踪用户输入之间的时间。

    我忘记了
    块信号
    ,但在这种情况下,出于某种原因,它没有提供所需的效果。经过一点尝试,我成功地将Google示例(在一个轻量级版本中)移植到PyQt。好提示。@embert那么我认为你应该接受这个解决方案,不是吗?(除非等待更多答案)。