PYQT5 Learning Notes----PYQT inter-thread communication

Source: Internet
Author: User
Tags emit sin

Signals (Singal) and slots (slots) are used to communicate with each other, signaling that when an event of an object occurs, a signal is triggered, a slot: the response to the specified signal, in fact the signal slot is similar. NET delegate, events, such as the Repeater control class, when the row data binding, triggering a ItemDataBound event, regardless of the user's use will listen to the event and do additional processing, its control class inside will trigger the event, this mechanism to many extent improve the encapsulation and integrity of the class.
PYQT's form control class already has a lot of built-in signals, and developers can add their own custom signals, and the signal slots have the following features:
-a signal can be connected to many slots.
-One signal can also be connected to another signal.
-the signal parameter can be any Python type.
-A slot can be connected to a number of signals.
-The connection may be direct (that is, synchronous) or queued (that is, asynchronous).
-Connections may cross threads.
-The signal may be disconnected

(The above features are translated into official documents), next, I will take a number of examples to reflect the above characteristics.

Use of built-in signal slots

From pyqt5.qtwidgets import *from pyqt5.qtcore import *  def sintest ():    btn.settext ("button text change")  app = Qapplication ([])  main = Qwidget () main.resize (200,100) btn = Qpushbutton ("button text", main) # # The built-in signal of the button btn is connected with a slot named Sintest btn.clicked.connect (sintest) main.show ()  app.exec_ ()

Use of custom signal slots
Class Sinclass (Qobject): # #声明一个无参数的信号 sin1 = pyqtsignal () # #声明带一个int类型参数的信号 sin2 = pyqtsignal (int) # #声明带一个int和str类型参数的信号 sin3 = pyqtsignal (int,str) # #声明带一个列表类型参数的信号 sin4 = pyqtsignal (list) # #声明带一  A signal of the dictionary type parameter sin5 = pyqtsignal (dict) # #声明一个多重载版本的信号, including a signal with an int and str type parameter, and a signal with the str parameter sin6 = pyqtsignal ([Int,str],          [STR]) def __init__ (Self,parent=none): Super (Sinclass,self). __init__ (parent) # #信号连接到指定槽 Self.sin1.connect ( Self.sin1call) Self.sin2.connect (self.sin2call) self.sin3.connect (self.sin3call) self.sin4.connect (SE Lf.sin4call) Self.sin5.connect (self.sin5call) self.sin6[int,str].connect (self.sin6call) self.sin6[str ].connect (self.sin6overload) # #信号发射 Self.sin1.emit () self.sin2.emit (1) self.sin3.emit (1, "Tex T ") Self.sin4.emit ([1,2,3,4]) self.sin5.emit ({" Name ":" Codeio "," Age ":" "}") Self.sin6[int,str].emit (1, "Text")        Self.sin6[str].emit ("text") def Sin1call (self): print ("Sin1 emit") def Sin2call (Self,val) : Print ("sin2 emit,value:", Val) def Sin3call (self,val,text): Print ("Sin3 emit,value:", Val,text) de F Sin4call (Self,val): Print ("Sin4 emit,value:", Val) def Sin5call (self,val): Print ("Sin5 Emit,va        Lue: ", Val) def Sin6call (self,val,text): Print (" Sin6 emit,value: ", Val,text) def sin6overload (Self,val): Print ("Sin6 overload Emit,value:", val) sin = Sinclass ()
Operation Result:
Sin1 Emit
Sin2 emit,value:1
Sin3 emit,value:1 Text
Sin4 Emit,value: [1, 2, 3, 4]
Sin5 emit,value: {' age ': ' + ', ' name ': ' Codeio '}
Sin6 emit,value:1 Text
SIN6 Overload Emit,value:text
Signal slot N to n connection, disconnect

From pyqt5.qtwidgets import *from pyqt5.qtcore import *class sinclass (qobject):    # #声明一个无参数的信号    sin1 = pyqtsignal (    # #声明带一个int类型参数的信号    sin2 = pyqtsignal (int)    def __init__ (self,parent=none):        super (Sinclass,self). _ _init__ (parent)        # #信号sin1连接到sin1Call和sin2Call这两个槽        self.sin1.connect (self.sin1call)        Self.sin1.connect (Self.sin2call)        # #信号sin2连接到信号sin1        self.sin2.connect (self.sin1)        # #信号发射        Self.sin1.emit ()        self.sin2.emit (1)        # #断开sin1, sin2 signal connection to each slot        Self.sin1.disconnect (self.sin1call)        Self.sin1.disconnect (Self.sin2call)        self.sin2.disconnect (self.sin1)        # # Signal Sin1 and sin2 are connected to the same slot Sin1call        self.sin1.connect (self.sin1call)        self.sin2.connect (Self.sin1call)        # # Signal is fired again        self.sin1.emit ()        self.sin2.emit (1)    def sin1call (self):        print ("Sin1 emit")    def Sin2call (self):        print ("sin2 emit") sin = Sinclass ()
Operation Result:
Sin1 Emit
Sin2 emit
Sin1 Emit
Sin2 emit
Sin1 Emit
Sin1 Emit

Multi-threaded signal slot communication
from pyqt5.qtwidgets import *from pyqt5.qtcore import *class Main (qwidget): Def __init_ _ (self, parent = None): Super (Main,self). __init__ (parent) # #创建一个线程实例并设置名称, variable, signal slot Self.thread = Mythre AD () self.thread.setIdentity ("Thread1") Self.thread.sinOut.connect (Self.outtext) Self.thread.setVal (6 ) def outtext (self,text): Print (Text) class MyThread (qthread): Sinout = pyqtsignal (str) def __init__ (self,p        Arent=none): Super (Mythread,self). __init__ (parent) self.identity = None def setidentity (self,text): self.identity = text def setval (self,val): self.times = Int (val) # #执行线程的run方法 Self.start () de F Run (self): while Self.times > 0 and self.identity: # #发射信号 Self.sinOut.emit (self.identity + "" +str (self.times)) self.times-= 1app = Qapplication ([]) main = Main () main.show () app.exec_ () 
Operation Result:
Thread1 6
Thread1 5
Thread1 4
Thread1 3
Thread1 2
Thread1 1

PYQT5 Learning Notes----PYQT inter-thread communication

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.