The problem is caused by deleting the list of searcher objects every time a search is done, which is unnecessary.
You only need to create the searcher objects once, and then make requests to them by emitting signalSearch
with the required word.
So the searcher class would look like this:
class Searcher(QObject):
signalUpdate = pyqtSignal(str, str)
def __init__(self, dictionary):
super(Searcher, self).__init__()
self.dictionary = dictionary
# word is sent by signalSearch
def run(self, word):
self.signalUpdate.emit(self.dictionary, word)
and the main window class would look like this:
class MyMainWindow(QMainWindow, Ui_MainWindow):
signalSearch = pyqtSignal(str)
def __init__(self, parent=None):
super(MyMainWindow, self).__init__()
self.setupUi(self)
self.dictList = ['Yahoo dictionary', 'Webster dictionary']
self.searchThreads = []
self.searchers = []
# create all the threads and searchers once
for dictionary in self.dictList:
thread = QThread(self)
self.searchThreads.append(thread)
searcher = Searcher(dictionary)
self.searchers.append(searcher)
searcher.signalUpdate.connect(self.updateGUI)
self.signalSearch.connect(searcher.run)
searcher.moveToThread(thread)
thread.start()
def click(self):
# send a new word to the searchers
self.signalSearch.emit(self.lineEdit.text())
def updateGUI(self, dictionary, word):
print(dictionary+'['+word+']')