from PyQt4.QtGui import QWidget,QLabel,QDateEdit, QLineEdit, QApplication, QPushButton, QVBoxLayout
from PyQt4.QtCore import pyqtSignal as Signal, pyqtSlot as Slot
# from PyQt5.QtWidgets import QWidget,QLabel,QDateEdit, QLineEdit, QApplication, QPushButton, QVBoxLayout
# from PyQt5.QtCore import pyqtSignal as Signal,pyqtSlot as Slot
# from PySide2.QtWidgets import QWidget,QLabel,QDateEdit, QLineEdit, QApplication, QPushButton, QVBoxLayout
# from PySide2.QtCore import Signal,Slot
import sys,os
class TestWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()
self.count = 0
#定义一个型号
test_signal = Signal(int)
#信号发送器
def on_button_clicked(self):
#self.close()
self.count += 1
self.test_signal.emit(self.count)
# 信号接收器
@Slot(int)
def signal_receiver_test(self, value):
self.editor.setText(str(value))
def initUI(self):
self.layout = QVBoxLayout()
self.btn = QPushButton("信号发送测试(Qt Signal Test)", self)
self.editor = QLineEdit("", self)
self.layout.addWidget(self.btn)
self.layout.addWidget(self.editor)
self.setLayout(self.layout)
self.btn.clicked.connect(self.on_button_clicked)
self.test_signal.connect(self.signal_receiver_test)
if __name__ == '__main__':
app = QApplication(sys.argv)
edit = TestWidget()
edit.show()
sys.exit(app.exec_())
一行一行敲出来的 ^_^
PyQt5/PySide2信号槽统一的代码参考了: https://www.learnpyqt.com/blog/pyqt5-vs-pyside2/
离线
离线
# from PyQt4.QtGui import QWidget,QLabel,QDateEdit, QLineEdit, QApplication, QPushButton, QVBoxLayout
# from PyQt4.QtCore import pyqtSignal as Signal, pyqtSlot as Slot
# from PyQt5.QtWidgets import QWidget,QLabel,QDateEdit, QLineEdit, QApplication, QPushButton, QVBoxLayout
# from PyQt5.QtCore import pyqtSignal as Signal,pyqtSlot as Slot
# from PySide2.QtWidgets import QWidget,QLabel,QDateEdit, QLineEdit, QApplication, QPushButton, QVBoxLayout
# from PySide2.QtCore import Signal,Slot
from Qt.QtWidgets import QWidget,QLabel,QDateEdit, QLineEdit, QApplication, QPushButton, QVBoxLayout
from Qt.QtCore import Signal,Slot
import sys,os
class TestWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()
self.count = 0
#定义一个型号
test_signal = Signal(int)
#信号发送器
def on_button_clicked(self):
#self.close()
self.count += 1
self.test_signal.emit(self.count)
# 信号接收器
@Slot(int)
def signal_receiver_test(self, value):
self.editor.setText(str(value))
def initUI(self):
self.layout = QVBoxLayout()
self.btn = QPushButton("信号发送测试(Qt Signal Test)", self)
self.editor = QLineEdit("", self)
self.layout.addWidget(self.btn)
self.layout.addWidget(self.editor)
self.setLayout(self.layout)
self.btn.clicked.connect(self.on_button_clicked)
self.test_signal.connect(self.signal_receiver_test)
if __name__ == '__main__':
app = QApplication(sys.argv)
edit = TestWidget()
edit.show()
sys.exit(app.exec_())
也兼容 Qt.Py
^_^
离线