pyqt5中QThread在使用时出现重复emit的实例

在PyQt5中使用QThread的时候,要注意把所有QThread的对象在主类中的init(或者放在所有类函数的外面)中进行实例化,不然可能在多个QThread互相调用的时候,emit重复的信号。一般比较正确的写法如下所示,基本照着这种框架搭建都是没问题的。

# -*- coding: utf-8 -*-
import sys
import time
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
 
 
class MainUi(QWidget):
  def __init__(self):
    super(MainUi, self).__init__()
    self.resize(400, 200)
    self.setWindowTitle('QThread例子')
 
    # 实例化多线程对象
    self.thread = Worker()
 
    # 实例化列表控件与按钮控件
    self.listFile = QListWidget()
    self.btnStart = QPushButton('开始')
    self.btn_over = QPushButton('结束')
 
    # 把控件放置在栅格布局中
    layout = QGridLayout(self)
    layout.addWidget(self.listFile, 0, 0, 1, 2)
    layout.addWidget(self.btnStart, 1, 0)
    layout.addWidget(self.btn_over, 1, 1)
 
    # 信号与槽函数的连接
    self.btnStart.clicked.connect(self.slotStart)
    self.btn_over.clicked.connect(self.slot_btn_over)
 
    # 建立线程信号的槽连接
    self.thread.trigger.connect(self.slotAdd)
 
  def slotAdd(self, msg):
    print(msg)
    if int(msg) % 2 == 0:
      self.listFile.addItem(msg)
    else:
      pass
    self.thread.exit()
 
  def slotStart(self):
    self.btnStart.setEnabled(False)
    self.thread.start()
 
  def slot_btn_over(self):
    self.btnStart.setEnabled(True)
    self.thread.terminate()
    self.thread.num = 0
 
 
class Worker(QThread):
  trigger = pyqtSignal(str)
  num = 0
 
  def __init__(self):
    super(Worker, self).__init__()
 
  def run(self):
    while True:
      print('num= ', self.num)
      if self.num % 2 == 0:
        self.trigger.emit(str(50))
      elif self.num == 200:
        self.num = 0
      else:
        pass
      time.sleep(0.1)
      self.num += 1
 
 
if __name__ == '__main__':
  app = QApplication(sys.argv)
  w = MainUi()
  w.show()
  sys.exit(app.exec_())

相关推荐