In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to customize the signal in Python PyQt5. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
PyQ5 has automatically defined a lot of QT built-in signals. However, in order to flexibly use the signal and slot mechanism in practical use, the signal can be customized according to the need. By using the pyqtSignal () method to define a new signal, the new signal is used as an attribute of the class.
Custom signal description:
The new signal should be defined in a subclass of QObject. The new signal must be part of the definition class, and it is not allowed to add the signal as an attribute of the class dynamically after the class definition. In this way, new signals can be automatically added to the QMetaObject class. This means that the newly defined signal will appear in Qt Designer and can be introspected through QMetaObject API.
The transmission of custom signal is realized by emit () method class
The general process of customizing a signal is as follows:
Define signal
Define slot function
Bind signals and slots
Transmit signal
Code example
Import sysfrom PyQt5.QtCore import pyqtSignal, QObject, Qt, pyqtSlotfrom PyQt5.QtWidgets import QWidget, QApplication, QGroupBox, QPushButton, QLabel, QCheckBox, QSpinBox, QHBoxLayout, QComboBox, QGridLayout class SignalEmit (QWidget): helpSignal = pyqtSignal (str) printSignal = pyqtSignal (list) # declares a multiload version of the signal, including a signal with int and str type parameters And the signal previewSignal = pyqtSignal with str parameter ([int,str]) [str]) def _ init__ (self): super (). _ init__ () self.initUI () def initUI (self): self.creatContorls ("print control:") self.creatResult ("operation result:") layout = QHBoxLayout () layout.addWidget (self.controlsGroup) layout.addWidget ( Self.resultGroup) self.setLayout (layout) self.helpSignal.connect (self.showHelpMessage) self.printSignal.connect (self.printPaper) self.previewSigna [str] .connect (self.previewPaper) self.previewSignal [int Str] .connect (self.previewPaperWithArgs) self.printButton.clicked.connect (self.emitPrintSignal) self.previewButton.clicked.connect (self.emitPreviewSignal) self.setGeometry (300,300,290,150) self.setWindowTitle ('defined signal') self.show () def creatContorls (self Title): self.controlsGroup = QGroupBox (title) self.printButton = QPushButton ("print") self.previewButton = QPushButton ("Preview") numberLabel = QLabel ("number of copies:") pageLabel = QLabel ("Paper Type:") self.previewStatus = QCheckBox ("full screen Preview") self.numberSpinBox = QSpinBox () self.numberSpinBox.setRange (1 Self.styleCombo = QComboBox (self) self.styleCombo.addItem ("A4") self.styleCombo.addItem ("A5") controlsLayout = QGridLayout () controlsLayout.addWidget (numberLabel, 0,0) controlsLayout.addWidget (self.numberSpinBox, 0,1) controlsLayout.addWidget (pageLabel, 0,2) controlsLayout.addWidget (self.styleCombo, 0,3) controlsLayout.addWidget (self.printButton, 0 4) controlsLayout.addWidget (self.previewStatus, 3,0) controlsLayout.addWidget (self.previewButton, 3,1) self.controlsGroup.setLayout (controlsLayout) def creatResult (self Title): self.resultGroup = QGroupBox (title) self.resultLabel = QLabel ("") layout = QHBoxLayout () layout.addWidget (self.resultLabel) self.resultGroup.setLayout (layout) def emitPreviewSignal (self): if self.previewStatus.isChecked () = True: self.previewSignal [int,str] .emit (1080 "Full Screen") elif self.previewStatus.isChecked () = False: self.previewSigna [str] .emit ("Preview") def emitPrintSignal (self): pList = [] pList.append (self.numberSpinBox.value ()) pList.append (self.styleCombo.currentText ()) self.printSignal.emit (pList) def printPaper (self List): self.resultLabel.setText ("Print:" + "copies:" + str (list [0]) + "Paper:" + str (list [1])) def previewPaperWithArgs (self,style,text): self.resultLabel.setText (str (style) + text) def previewPaper (self,text): self.resultLabel.setText (text) def keyPressEvent (self) Event): if event.key () = = Qt.Key_F1: self.helpSignal.emit ("help message") def showHelpMessage (self,message): self.resultLabel.setText (message) # self.statusBar (). ShowMessage (message) if _ _ name__ ='_ main__': app = QApplication (sys.argv) dispatch = SignalEmit () sys.exit (app.exec_ ())
Sample description:
Through an analog printing interface to explain in detail about the signal customization, when printing can set the print score, paper type, trigger the "print" button, the implementation results will be displayed to the right; through full-screen preview QCheckBox to choose whether to preview through full-screen mode, the implementation results will be displayed to the right.
HelpMessage information can be displayed by clicking the F1 shortcut key.
Interface analysis:
The interface is mainly composed of two parts: one is the print control, the other is the operation result.
Combined with QHBoxLayout, as follows:
Layout = QHBoxLayout () layout.addWidget (self.controlsGroup) layout.addWidget (self.resultGroup) self.setLayout (layout)
Then define the "print control" interface through creatContorls
Def creatContorls (self,title): self.controlsGroup = QGroupBox (title) self.printButton = QPushButton ("print") self.previewButton = QPushButton ("Preview") numberLabel = QLabel ("number of copies:") pageLabel = QLabel ("Paper Type:") self.previewStatus = QCheckBox ("full screen Preview") self.numberSpinBox = QSpinBox () self.numberSpinBox.setRange (1 Self.styleCombo = QComboBox (self) self.styleCombo.addItem ("A4") self.styleCombo.addItem ("A5") controlsLayout = QGridLayout () controlsLayout.addWidget (numberLabel, 0,0) controlsLayout.addWidget (self.numberSpinBox, 0,1) controlsLayout.addWidget (pageLabel, 0,2) controlsLayout.addWidget (self.styleCombo, 0,3) controlsLayout.addWidget (self.printButton, 0,4) controlsLayout.addWidget (self.previewStatus, 3 0) controlsLayout.addWidget (self.previewButton, 3,1) self.controlsGroup.setLayout (controlsLayout)
QSpinBox is a counter control that allows the user to select an integer value by clicking up and down or pressing the up and down keys on the keyboard to increase or decrease the currently displayed value, of course, the user can also enter a value.
QComboBox is a control that combines buttons and drop-down options, also known as a drop-down list box.
Then define the "operation result" interface through creatResult:
Def creatResult (self,title): self.resultGroup = QGroupBox (title) self.resultLabel = QLabel ("") layout = QHBoxLayout () layout.addWidget (self.resultLabel) self.resultGroup.setLayout (layout)
Code analysis:
HelpSignal = pyqtSignal (str) printSignal = pyqtSignal (list) # declares a multi-overloaded version of the signal, including a signal with int and str type parameters, and a signal with str parameters previewSignal = pyqtSignal ([int,str], [str])
Three signals, helpSignal,printSignal,previewSignal, are defined by pyqtSignal (). Where:
HelpSignal is a signal of str parameter type.
PrintSignal is a signal of list parameter type.
PreviewSignal is a multi-overloaded version of the signal, including a signal with int and str type parameters, as well as parameters for str class lines.
Self.helpSignal.connect (self.showHelpMessage) self.printSignal.connect (self.printPaper) self.previewSigna [str] .connect (self.previewPaper) self.previewSignal [int,str] .connect (self.previewPaperWithArgs) self.printButton.clicked.connect (self.emitPrintSignal) self.previewButton.clicked.connect (self.emitPreviewSignal)
Bind signals and slots.
Focus on the binding of multiple overloaded versions of the signal. PreviewSignal has two versions, previewSignal (str) and previewSignal (int,str). Since there are two versions, the binding relationship between the signal and the slot needs to be explicitly specified at the time of binding.
The details are as follows:
Self.previewSigna [str] .connect (self.previewPaper) self.previewSignal [int,str] .connect (self.previewPaperWithArgs)
The previewSignal signal of [str] parameter is bound to previewPaper (), and the previewSignal signal of [int,str] is bound to previewPaperWithArgs ()
Def emitPreviewSignal (self): if self.previewStatus.isChecked () = = True: self.previewSignal [int,str] .emit (1080, "Full Screen") elif self.previewStatus.isChecked () = False: self.previewSignal.emit ("Preview")
The transmission of a multi-overloaded version of the signal also needs to develop a version corresponding to the transmission, similar to the version of the same signal.
Def emitPrintSignal (self): pList = [] pList.append (self.numberSpinBox.value ()) pList.append (self.styleCombo.currentText ()) self.printSignal.emit (pList)
As shown in the code, parameters of the python data type can be passed when the signal is transmitted, and in this case, the parameter pList of type list can be passed.
Def keyPressEvent (self, event): if event.key () = = Qt.Key_F1: self.helpSignal.emit ("help message")
The function of F1 shortcut key is extended by copying keyPressEvent () method. In most applications of windows, we use some keyboard shortcuts to quickly complete certain functions. For example, the F1 key will quickly call up the help interface, so you can override the keyPressEvent () method to simulate sending the required signal to complete the corresponding task.
Note:
1. Custom signals are defined before the init () function
two。 Custom models can pass many types of parameters, such as str, int, list, object, float, tuple, dict, etc.
3. Pay attention to the calling logic of signal and slot to avoid an endless loop between signal and slot. Such as continuing to transmit the signal in the slot method
This is the end of this article on "how to customize signals in Python PyQt5". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.