欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

详解pyqt5 动画在QThread线程中无法运行问题

程序员文章站 2024-01-27 09:56:16
自己做了一个tcp工具,在学习动画的时候踩了坑,需求是根据上线变绿色,离线变灰色,如果连接断开了,则变为灰色 问题现象: 可以看到点击“连接”,“离线”的时候动画是...

自己做了一个tcp工具,在学习动画的时候踩了坑,需求是根据上线变绿色,离线变灰色,如果连接断开了,则变为灰色

问题现象:

详解pyqt5 动画在QThread线程中无法运行问题

可以看到点击“连接”,“离线”的时候动画是正常的,但是当tcp超时断开后,虽然离线按钮变为连接了,却没有执行离线动画

关键源代码如下

class bsjtcpthread(qtcore.qthread):
  recv_signal = qtcore.pyqtsignal(str)
  send_signal = qtcore.pyqtsignal(str)

  def __init__(self, socketcp, onbtn, heartcheck, senbtn, scene):
    super().__init__()
    self.s = socketcp
    self.yqtool = bianlifunction()
    self.onbtn = onbtn
    self.heartcheck = heartcheck
    self.sendbtn = senbtn
    self.scene1 = scene

  def run(self):
    """线程"""
    global stopsingle
    stopsingle = 0
    while 1:
      btcpreceive = self.s.recv(1024)
      tcpreceive1 = str(binascii.b2a_hex(btcpreceive), encoding="utf-8")

      tcpreceive = ""
      i = 0
      while i < len(tcpreceive1) - 1: # 十六进制数据处理,两个字节隔开
        if i == len(tcpreceive1) - 2:
          tcpreceive += tcpreceive1[i:i + 2]
          i += 2
        else:
          tcpreceive += tcpreceive1[i:i + 2] + " "
          i += 2

      if tcpreceive == "":
        stopsingle = 1
        self.s.shutdown(2)
        self.s.close()
        self.onbtn.settext("连接")
        self.scene1.offlinecol.start() # 启动离线动画
        self.heartcheck.setchecked(false)
        self.heartcheck.setvisible(false)
        self.sendbtn.setdisabled(true)
      else:
        self.recv_signal.emit(tcpreceive)
      if stopsingle == 1:
        break

然后再启动线程

      self.tcpth = bsjtcpthread(self.s, self.onbtn, self.heartcheck, self.sendbtn, self.scene)
      self.tcpth.recv_signal.connect(self.fillrecvmsg)
      self.tcpth.send_signal.connect(self.fillsendmsg)
      self.tcpth.start()

问题点:

经过谷爹搜索,终于找到了问题原因详见https://*.com/questions/44328750/pyqt-qgraphicscene-move-item-in-background-thread

大致原因就是qgraphics scene 不是一个安全的线程对象,我们不能直接在线程中去改变主程序的状态,我们必须通过信号的方式去更新qgraphics

解决方法:

首先,我们编辑一个信号方法

  def threadanimate(self, message):
    if message == "1":
      self.scene.offlinecol.start()

然后添加相关信号槽

self.tcpth = bsjtcpthread(self.s, self.onbtn, self.heartcheck, self.sendbtn)
      self.tcpth.recv_signal.connect(self.fillrecvmsg)
      self.tcpth.send_signal.connect(self.fillsendmsg)
      self.tcpth.animate_signal.connect(self.threadanimate) # 添加一个动画信号
      self.tcpth.start()

在线程中发出离线动画的信号

class bsjtcpthread(qtcore.qthread):
  recv_signal = qtcore.pyqtsignal(str)
  send_signal = qtcore.pyqtsignal(str)
  animate_signal = qtcore.pyqtsignal(str)

  def __init__(self, socketcp, onbtn, heartcheck, senbtn):
    super().__init__()
    self.s = socketcp
    self.yqtool = bianlifunction()
    self.onbtn = onbtn
    self.heartcheck = heartcheck
    self.sendbtn = senbtn

  def run(self):
    """线程"""
    global stopsingle
    stopsingle = 0
    while 1:
      btcpreceive = self.s.recv(1024)
      tcpreceive1 = str(binascii.b2a_hex(btcpreceive), encoding="utf-8")

      tcpreceive = ""
      i = 0
      while i < len(tcpreceive1) - 1: # 十六进制数据处理,两个字节隔开
        if i == len(tcpreceive1) - 2:
          tcpreceive += tcpreceive1[i:i + 2]
          i += 2
        else:
          tcpreceive += tcpreceive1[i:i + 2] + " "
          i += 2

      if tcpreceive == "":
        stopsingle = 1
        self.s.shutdown(2)
        self.s.close()
        self.onbtn.settext("连接")
        self.animate_signal.emit("1")
        self.heartcheck.setchecked(false)
        self.heartcheck.setvisible(false)
        self.sendbtn.setdisabled(true)
      else:
        self.recv_signal.emit(tcpreceive)
      if stopsingle == 1:
        break

然后就可以了,这个和qthread多线程收发消息原理一样

详解pyqt5 动画在QThread线程中无法运行问题

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。