如何在 PyQt 中连接 Signal 和 Slot
How to connect the Signal an the Slot in PyQt
我的编译环境是PyQt4,Qt4,Python2.In我的代码,有一个Signal:
class ReadThread(QtCore.QThread):
#always read UART RX pin
def __init__(self,parent=None):
QtCore.QThread.__init__(self,parent)
self.trigger=QtCore.pyqtSignal()#creat a signal
def run(self):
#thread stop when the "run" function is over
for i in range (25536):
pass
self.trigger.emit()
并且 Class ChatDialog 中有一个 Slot()。
class ChatDialog(QtGui.QDialog):
#dialog contain two widget-"recived"and"send"
def __init__(self,parent=None):
QtGui.QDialog.__init__(self,parent)
self.ui=Ui_Dialog()
self.ui.setupUi(self)
@QtCore.pyqtSlot()
def print_slot():
print "reciece str"
我写__main__like这个:
if __name__=='__main__':
app = QtGui.QApplication(sys.argv)
myqq=ChatDialog()
myqq.show()
read=ReadThread()
read.trigger.connect(myqq.print_slot,QueuedConnection)
read.start()
sys.exit(app.exec_())
但是我的 "read.trigger.connect(myqq.print_slot,QueuedConnection)" 是 wrong.How 我可以连接 Signal 和 Slot 吗?谢谢
信号必须定义为 class 属性。您不能在实例上创建信号。
class ReadThread(QtCore.QThread):
trigger = QtCore.pyqtSignal()
def __init__(self,parent=None):
QtCore.QThread.__init__(self,parent)
我的编译环境是PyQt4,Qt4,Python2.In我的代码,有一个Signal:
class ReadThread(QtCore.QThread):
#always read UART RX pin
def __init__(self,parent=None):
QtCore.QThread.__init__(self,parent)
self.trigger=QtCore.pyqtSignal()#creat a signal
def run(self):
#thread stop when the "run" function is over
for i in range (25536):
pass
self.trigger.emit()
并且 Class ChatDialog 中有一个 Slot()。
class ChatDialog(QtGui.QDialog):
#dialog contain two widget-"recived"and"send"
def __init__(self,parent=None):
QtGui.QDialog.__init__(self,parent)
self.ui=Ui_Dialog()
self.ui.setupUi(self)
@QtCore.pyqtSlot()
def print_slot():
print "reciece str"
我写__main__like这个:
if __name__=='__main__':
app = QtGui.QApplication(sys.argv)
myqq=ChatDialog()
myqq.show()
read=ReadThread()
read.trigger.connect(myqq.print_slot,QueuedConnection)
read.start()
sys.exit(app.exec_())
但是我的 "read.trigger.connect(myqq.print_slot,QueuedConnection)" 是 wrong.How 我可以连接 Signal 和 Slot 吗?谢谢
信号必须定义为 class 属性。您不能在实例上创建信号。
class ReadThread(QtCore.QThread):
trigger = QtCore.pyqtSignal()
def __init__(self,parent=None):
QtCore.QThread.__init__(self,parent)