PyQt的signal 和 solit的补充

时间:2023-03-09 14:47:43
PyQt的signal 和 solit的补充
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_())