自己做了一个tcp工具,在学习动画的时候踩了坑,需求是根据上线变绿色,离线变灰色,如果连接断开了,则变为灰色
问题现象:
可以看到点击“连接”,“离线”的时候动画是正常的,但是当tcp超时断开后,虽然离线按钮变为连接了,却没有执行离线动画
关键源代码如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
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 |
然后再启动线程
1
2
3
4
|
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://stackoverflow.com/questions/44328750/pyqt-qgraphicscene-move-item-in-background-thread
大致原因就是qgraphics scene 不是一个安全的线程对象,我们不能直接在线程中去改变主程序的状态,我们必须通过信号的方式去更新qgraphics
解决方法:
首先,我们编辑一个信号方法
1
2
3
|
def threadanimate( self , message): if message = = "1" : self .scene.offlinecol.start() |
然后添加相关信号槽
1
2
3
4
5
|
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() |
在线程中发出离线动画的信号
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
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多线程收发消息原理一样
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://www.cnblogs.com/semishigure/p/8986088.html