In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you the "sample analysis of decorator signals and slots in python". The content is simple and easy to understand, and the organization is clear. I hope it can help you solve your doubts. Let Xiaobian lead you to study and learn the "sample analysis of decorator signals and slots in python" article.
Decorator signal and groove
The so-called decorator signal and groove, that is, through the decorator method to define the signal and groove function, the specific use method is as follows
@PyQt5.QtCore.pyqtSlot(parameter)def on_sender object name_emission signal name (self, parameter): pass
This method works if the following function has been executed:
QMetaObject.connectSlotsByName(QObject)
In the above code,'sender object name' is the name set using setObjectName function, so the naming convention of custom slot function can also be regarded as: on+ name set using setObjectName + signal name
Here are some concrete examples.
import sysfrom PyQt5.QtWidgets import QApplication,QWidget,QHBoxLayout,QPushButtonfrom PyQt5 import QtCoreclass CustWidget(QWidget): def __init__(self,parent=None): super(CustWidget, self).__ init__(parent) #Create button, add to self window self.okButton=QPushButton ('ok ',self) #Set object name with setObjectName self.okButton.setObjectName ('okButton ') #Set its layout to horizontal, and add button controls to it layout=QHBoxLayout(self) layout.addWidget (self.okButton) #TODo First Method #self.okButton.clicked.connect (self.okButton_clicked) #def okButton_clicked( self ): # print ('ok button')#Second method QtCore.QMetaObject.connectSlotsByName(self) @QtCore.pyqtSlot() def on_okButton_clicked( self ): print ('ok button') if __name__ == '__main__': app=QApplication(sys.argv) win=CustWidget() win.show () sys.exit(app.exec_())
Run the program, display the effect as shown in the figure, click the ok button, the console prints the expected debugging information
QtCore.QMetaObject.connectSlotsByName(self)
In fact, it is the core code in PyQt5 that automatically connects to slot functions based on signal names. This line of code is used to connect certain objects of descendant objects in QObject to corresponding slot functions according to their objectName. For example, name
Assuming the code QtCore.QMetaObject.connectSlotsByName(self) has been executed, the following code
@QtCore.pyqtSlot()
def on_okButton_clicked( self ):
print ('OK button clicked ')
is automatically recognized as the following code:
def __init__(self,parent=None):
self.okButton.clicked.connect(self.okButton_clicked)
def okButton_clicked(self):
print ('OK button clicked ')
The above is "python decorator signal and groove example analysis" all the contents of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!
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.