PyQt5 Study Notes 13 ---- pyqt thread communication, pyqt513 ---- pyqt

Source: Internet
Author: User

PyQt5 Study Notes 13 ---- pyqt thread communication, pyqt513 ---- pyqt
Singal and slot are used for communication between objects. Signal: when an event of an object occurs, a signal is triggered. slot: responds to the response of a specified signal, in fact, the signal slot is similar. the delegate and event in. NET, such as the Repeater control class, triggers an ItemDataBound event after the row data is bound. No matter the user uses it, It listens to the event and performs additional processing, the event is triggered inside the control class. This mechanism improves the encapsulation and integrity of the class to a large extent.
PyQt's form control class already has many built-in signals. developers can also add their own custom signals. The signal slots have the following features:
-One signal can be connected to many slots.
-One signal can be connected to another signal.
-The signal parameter can be of any Python type.
-A single slot can be connected to many signals.
-The connection may be directly (synchronous) or queued (asynchronous ).
-The connection may be cross-thread.
-The signal may be disconnected.

(The above features are translated into official documents). Next, I will use several examples to reflect the above features.

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 connects to the slot btn named sinTest. clicked. connect (sinTest) main. show () app.exe c _()

Use of custom signal Slots
Class SinClass (QObject): # declare a signal without parameters sin1 = pyqtSignal () # declare a signal with an int type parameter sin2 = pyqtSignal (int) # declare a signal with an int or str type parameter sin3 = pyqtSignal (int, str) # declare a signal with a list type parameter sin4 = pyqtSignal (list) # declare a signal with a dictionary type parameter sin5 = pyqtSignal (dict) # declare a signal with multiple overloaded versions, including a signal with int and str parameters, and the signal sin6 = pyqtSignal ([int, str], [str]) def _ init _ (self, parent = None): super (SinClass, self ). _ init _ (parent) # connect the signal to the specified slot self. sin1.connect (self. sin1Call) self. sin2.connect (self. sin2Call) self. sin3.connect (self. sin3Call) self. sin4.connect (self. sin4Call) self. sin5.connect (self. sin5Call) self. sin6 [int, str]. connect (self. sin6Call) self. sin6 [str]. connect (self. sin6OverLoad) # signal emission self. sin1.emit () self. sin2.emit (1) self. sin3.emit (1, "text") self. sin4.emit ([1, 2, 3, 4]) self. sin5.emit ({"name": "codeio", "age": "25"}) 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) def sin4Call (self, val): print ("sin4 emit, value: ", val) def sin5Call (self, val): print (" sin5 emit, value: ", val) def sin6Call (self, val, text): print (" sin6 emit, value: ", val, text) def sin6OverLoad (self, val): print (" sin6 overload emit, value: ", val) sin = SinClass ()
Running result:
Sin1 emit
Sin2 emit, value: 1
Sin3 emit, value: 1 text
Sin4 emit, value: [1, 2, 3, 4]
Sin5 emit, value: {'age': '25', 'name': 'codeio '}
Sin6 emit, value: 1 text
Sin6 overload emit, value: text
Signal slot N to N connections and disconnection

From PyQt5.QtWidgets import * from PyQt5.QtCore import * class SinClass (QObject): # declare a nonparametric signal sin1 = pyqtSignal () # declare the signal sin2 = pyqtSignal (int) def _ init _ (self, parent = None) with an int type parameter: super (SinClass, self ). _ init _ (parent) # signal sin1 is connected to the sin1Call and sin2Call slots self. sin1.connect (self. sin1Call) self. sin1.connect (self. sin2Call) # signal sin2 connected to signal sin1 self. sin2.connect (self. sin1) # signal emission self. sin1.emit () self. sin2.emit (1) # Disconnect sin1 and sin2 signals from 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 sin1Call self. sin1.connect (self. sin1Call) self. sin2.connect (self. sin1Call) # signal re-launch self. sin1.emit () self. sin2.emit (1) def sin1Call (self): print ("sin1 emit") def sin2Call (self): print ("sin2 emit") sin = SinClass ()
Running result:
Sin1 emit
Sin2 emit
Sin1 emit
Sin2 emit
Sin1 emit
Sin1 emit

Multi-thread signal slot Communication
From PyQt5.QtWidgets import * from PyQt5.QtCore import * class Main (QWidget): def _ init _ (self, parent = None): super (Main, self ). _ init _ (parent) # create a thread instance and set the name, variable, and signal slot self. thread = MyThread () 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, parent = 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 method self of the execution thread. start () def run (self): while self. times> 0 and self. identity: # transmit signal self. sinOut. emit (self. identity + "" + str (self. times) self. times-= 1app = QApplication ([]) main = main(+main.show(+app.exe c _()
Running result:
Thread1 6
Thread1 5
Thread1 4
Thread1 3
Thread1 2
Thread1 1

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.