zoukankan      html  css  js  c++  java
  • QtCore.QMetaObject.connectSlotsByName:根据objectName和signal自动绑定slot

    from PyQt5.QtWidgets import (QWidget
                , QVBoxLayout , QHBoxLayout, 
                 QLineEdit, QPushButton)
    
    from PyQt5.QtCore import  pyqtSignal
    from PyQt5 import QtCore
    ##############################################
    
    # 参考大丸子的博客
    #http://jimmykuu.sinaapp.com/blog/11
    
    
    
    
    
    
    class LoginView(QWidget):
        
        ## 登录界面时发送 关闭信号
        quitSignal = pyqtSignal()
        loginSignal = pyqtSignal(list)
        # server端没有响应
        openFailureSignal = pyqtSignal()
        
        
    
        
        
        def __init__(self, parent=None):
            super(LoginView, self).__init__(parent)
            self.ids_receive = []
            
            self._init_ui()
    
    
            
        def _init_ui(self):    
    
            layout_button = QHBoxLayout()       
            layout_input = QHBoxLayout()       
            
            self.input_name = QLineEdit()
            self.input_pass = QLineEdit()
            
            
            button_login =  QPushButton("登录")
            button_login.setObjectName("ok_button")
            
            button_quit  =  QPushButton("取消")
            button_hello  =  QPushButton("hello")
            self.button_hello = button_hello
            button_hello.setObjectName("hello_button")
    
            #button_login.clicked.connect(self.do_login)
            #button_quit.clicked.connect(self.loginQuit)
            
            layout_button.addWidget(button_login)
            layout_button.addWidget(button_quit)
            
            layout_input.addWidget(self.input_name)
            layout_input.addWidget(self.input_pass)
            layout_input.addWidget(self.button_hello)
            
      
            layout_main = QVBoxLayout()  
            self.setLayout(layout_main)
            layout_main.addLayout(layout_button)
            layout_main.addLayout(layout_input)
       
            # QMetaObject. connectSlotsByName(QObject)
            #网上百度到的说明:其作用是如其名称一样,用来将QObject 里的子孙QObject的某些信号按照其objectName连接到相应的槽上
            #       ,如 button_hello.setObjectName("hello_button")
            # 官网解释用法: http://doc.qt.io/qt-5/qmetaobject.html#connectSlotsByName
            QtCore.QMetaObject.connectSlotsByName(self)
         
        def loginQuit(self):
            print ("cencel")
        
        #试试注销掉该装饰器
        # 有点奇怪啊,一旦注释掉装饰器,打印会执行两次啊
        #QtCore.pyqtSlot(str, str)可以携带参数的
        @QtCore.pyqtSlot() 
        def on_hello_button_clicked(self):   
            print('on_pbHello_clicked')
           
        @QtCore.pyqtSlot()    
        def on_ok_button_clicked(self):
            print ("OK")
            self.loginSignal.emit([1, 2])
            
    
            
        def do_login(self):
            #获取用户和密码文本框的内容
            u_name = self.input_name.text()
            u_pass =  self.input_pass.text()
            
           
            print('u_name', u_name, u_pass)
            
            
    
            
    if __name__ == '__main__':
    
        import sys
        from PyQt5.QtWidgets import QApplication
        app = QApplication(sys.argv)    
    
        login = LoginView()
        login.show()
    
        sys.exit(app.exec_())
    

      

  • 相关阅读:
    parseInt 的第二个参数
    htm Dom对象与 Xml Dom对象的理解
    js 立即执行函数,() .则前面的function 是表达式,不能是函数申明
    Promise A 规范的一个简单的浏览器端实现
    css3,环绕圆环 loading,小组件
    有用的小程序的总结
    STM32定时器的小问题总结
    stm32 串口发送数据第一字节丢失
    嵌入式C语言编程小知识总结
    STM32多通道ADC操作
  • 原文地址:https://www.cnblogs.com/ribavnu/p/4893581.html
Copyright © 2011-2022 走看看