QThread :Destroyed while thread is still running

程序运行报错:QThread :Destroyed while thread is still running

pyqt在ui界面中,查db的大量数据会卡顿
用,必须用QThread解决
今天运行界面报错:QThread :Destroyed while thread is still running

解决方案:
self.
exec()

pyqt真呵呵…

pyqt多线程模块导入

from PyQt5.QtCore import Qt, QThread, pyqtSignal
# 控件的导入
from PyQt5.QtWidgets import QMessageBox, QCompleter


在主线程中:
1.实例化为实力参数self.
2.加exec()保护线程
3.代码不全,这是主线程中调用多线程的代码

        self.symbol_thread = GetSymbolListThread()
        self.symbol_thread.start()
        self.symbol_thread.exec()
        self.symbol_thread.bardata_signal.connect(self.emitbardata)
主线程中接受多线程传值的控件逻辑实现

    def emitbardata(self, symbol_list: list):
        self.completer = QCompleter(symbol_list)
        self.completer.setFilterMode(Qt.MatchContains)
        self.completer.setCompletionMode(QCompleter.PopupCompletion)
        self.symbol_line.setCompleter(self.completer)
多线程读取数据库代码实现

class GetSymbolListThread(QThread):
    bardata_signal = pyqtSignal(list)

    def __init__(self):
        super(GetSymbolListThread, self).__init__()
    #必须交run,内部实现叫run,重写也要叫run
    def run(self):
        baroverview = database_manager.get_bar_overview()
        vt_symbol_set = set()
        for item in baroverview:
            vt_symbol = f'{item.symbol}.{item.exchange.value}'
            vt_symbol_set.add(vt_symbol)
        symbol_list = list(vt_symbol_set)
        self.bardata_signal.emit(symbol_list)

你可能感兴趣的:(QThread :Destroyed while thread is still running)