分享

pyqt4中创建自定义signal的方法

 Tech-d 2013-01-29
class SSHThread(QThread):
    def __init__(self, hostname = '', renderList = [], tabLabelText = 'L0001', parent = None):
        """ constructor"""
        ...omit...
        #a new custom signal to set string to textEdit
        self.editSignal = pyqtSignal(QString, QString)
         ...omit...

    def run(self):
          """ emit the new signal"""
          ... omit ...
          self.emit(SIGNAL('editSignal(QString, QString)'), QString("param1"), QString("param2"))
          ... omit ...

...... omit ......
def executeBtn(self):
      """ connect the new signal and a slot function"""
      QObject.connect(self.sshThread, SIGNAL('editSignal(QString, QString)'), self.on_textEdit_update)
      ... omit ...

def on_textEdit_update(self, param1, param2):
       """ the slot function"""
       ... omit ...


================================================

20151223补充一个完整例子,来源:

http:///questions/3891465/how-to-connect-pyqtsignal-between-classes-in-pyqt

from PyQt4 import QtCore

class Pot(QtCore.QObject):

    temperatureRaisedSignal = QtCore.pyqtSignal()

    def __init__(self, parent=None):
        QtCore.QObject.__init__(self)
        self.temperature = 1

    def Boil(self):
        self.temperatureRaisedSignal.emit()
        self.temperature += 1

class Thermometer():
    def __init__(self, pot):
        self.pot = pot
        self.pot.temperatureRaisedSignal.connect(self.temperatureWarning)

    def StartMeasure(self):
        self.pot.Boil()

    def temperatureWarning(self):
        print("Too high temperature!")

if __name__ == '__main__':
    pot = Pot()
    th = Thermometer(pot)
    th.StartMeasure()



    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多