Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用PyQt信号插槽连接两个类?_Python_Python 2.7_Pyqt_Pyqt4_Twisted - Fatal编程技术网

Python 如何使用PyQt信号插槽连接两个类?

Python 如何使用PyQt信号插槽连接两个类?,python,python-2.7,pyqt,pyqt4,twisted,Python,Python 2.7,Pyqt,Pyqt4,Twisted,我正在使用twisted和PyQt用python编写一个应用程序。我想做的是,当客户端注册时,它会反映在前端。因此,当服务器注册客户端时,它会发出一个信号,该信号将被捕获并触发一个插槽,但它似乎不起作用。代码中的两个信号插槽均不工作 from PyQt4 import QtGui, QtCore import sys from twisted.internet.protocol import Factory, Protocol from twisted.protocols import amp

我正在使用twisted和PyQt用python编写一个应用程序。我想做的是,当客户端注册时,它会反映在前端。因此,当服务器注册客户端时,它会发出一个信号,该信号将被捕获并触发一个插槽,但它似乎不起作用。代码中的两个信号插槽均不工作

from PyQt4 import QtGui, QtCore
import sys
from twisted.internet.protocol import Factory, Protocol
from twisted.protocols import amp
import qt4reactor

class register_procedure(amp.Command):
    arguments = [('MAC',amp.String()),
                            ('IP',amp.String()),
                            ('Computer_Name',amp.String()),
                            ('OS',amp.String())
                            ]
    response = [('req_status', amp.String()),
             ('ALIGN_FUNCTION', amp.String()),
                             ('ALIGN_Confirmation', amp.Integer()),
                             ('Callback_offset',amp.Integer())
                            ]

class Ui_MainWindow(QtGui.QMainWindow):

    def __init__(self,reactor, parent=None):
        super(Ui_MainWindow,self).__init__(parent)
        self.reactor=reactor
        self.pf = Factory()
        self.pf.protocol = Protocol
        self.reactor.listenTCP(3610, self.pf) # listen on port 1234

    def setupUi(self,MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(903, 677)
        self.centralwidget = QtGui.QWidget(MainWindow)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.centralwidget.sizePolicy().hasHeightForWidth())
        self.centralwidget.setSizePolicy(sizePolicy)

        self.create_item()


        self.retranslateUi(MainWindow)
        self.connect(self, QtCore.SIGNAL('triggered()'), self.closeEvent)
        QtCore.QObject.connect(self,QtCore.SIGNAL('registered()'),self.registered)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
        self.pushButton_4.setText(_translate("MainWindow", "Delete System ", None))
        self.pushButton.setText(_translate("MainWindow", "Add System", None))
        self.label_2.setText(_translate("MainWindow", "SYSTEM STATUS", None))
        self.label.setText(_translate("MainWindow", "Monitoring Output", None))


    def registered(self):# this function is not being triggered
        print "check" 
        self.textbrowser.append()

    def closeEvent(self, event):#neither is this being triggered
        print "asdf"
        self.rector.stop()
        MainWindow.close()
        event.accept()

class bridge(QtCore.QObject):
    mySignal = QtCore.SIGNAL('mySignal(QString)')
    def __init__(self):
        QtCore.QObject.__init__(self)
    def registered(self):
        print "p op"
        QtCore.QObject.emit(self, QtCore.SIGNAL('registered'))


class Protocol(amp.AMP):
    @register_procedure.responder
    def register_procedure(self,MAC,IP,Computer_Name,OS):
        self.bridge_conn=bridge()
        cursor_device.execute("""select * FROM devices where MAC = ?;""",[(MAC)])
        exists_val=cursor_device.fetchone()
        cursor_device.fetchone()
        print "register"
        if not exists_val== "":
            cursor_device.execute("""update devices set IP= ? , Computer_name= ? , OS = ?  where MAC= ?;""",[IP,Computer_Name,OS,MAC])
            self.bridge_conn.registered() # <--emits signal
            return {'req_status': "done" ,'ALIGN_FUNCTION':'none','ALIGN_Confirmation':0,'Callback_offset':call_offset(1)}
        else:
            cursor_device.execute("""INSERT INTO devices(Mac,Ip,Computer_name,Os) values (?,?,?,?);""",[MAC,IP,Computer_Name,OS])
            self.bridge_conn.registered()#<--emits signal
            return {'req_status': "done" ,'ALIGN_FUNCTION':'main_loop()','ALIGN_Confirmation':0,'Callback_offset':0}



if  __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)

    try:
        import qt4reactor
    except ImportError:
        from twisted.internet import qt4reactor
    qt4reactor.install()

    from twisted.internet import reactor
    MainWindow = QtGui.QMainWindow() # <-- Instantiate QMainWindow object.
    ui = Ui_MainWindow(reactor)
    ui.setupUi(MainWindow)
    MainWindow.show()
    reactor.run()

下面是我连接两个类的方式,我不知道这是最好的方式,但它按照我的要求工作:

class MyForm(QtGui.QMainWindow):
    def __init__(self):
        ....
        self.connect(self, QtCore.SIGNAL("mySignal(PyQt_PyObject)"), self.doSomething)
    ....
    def someMethod(self):
       ...
       button = MyPushButton(i, self)
       ...
    def doSomething(self):
       ...

class MyPushButton(QtGui.QPushButton):
    def __init__(self, elementID, mForm):
        super(MyPushButton, self).__init__()
        self.__elementID = elementID
        self.__mForm = mForm
        self.connect(self, QtCore.SIGNAL('clicked()'),  self,       QtCore.SLOT("triggerOutput()"))

    @QtCore.pyqtSlot()
    def triggerOutput(self):
        self.__mForm.emit(QtCore.SIGNAL("mySignal(PyQt_PyObject)"), self.__elementID) 
所以,最重要的是最后一行,在这里我从MyButtown类self向MyForm类__mForm发出信号,并调用doSomething。但正如您在这行中看到的:button=MyPushButtoni,self,我已经将self-MyForm传递给了MyPushButton


我希望这能对你有所帮助。

以下是我连接两个类的方式,我不知道这是最好的方式,但它可以按照我的要求工作:

class MyForm(QtGui.QMainWindow):
    def __init__(self):
        ....
        self.connect(self, QtCore.SIGNAL("mySignal(PyQt_PyObject)"), self.doSomething)
    ....
    def someMethod(self):
       ...
       button = MyPushButton(i, self)
       ...
    def doSomething(self):
       ...

class MyPushButton(QtGui.QPushButton):
    def __init__(self, elementID, mForm):
        super(MyPushButton, self).__init__()
        self.__elementID = elementID
        self.__mForm = mForm
        self.connect(self, QtCore.SIGNAL('clicked()'),  self,       QtCore.SLOT("triggerOutput()"))

    @QtCore.pyqtSlot()
    def triggerOutput(self):
        self.__mForm.emit(QtCore.SIGNAL("mySignal(PyQt_PyObject)"), self.__elementID) 
所以,最重要的是最后一行,在这里我从MyButtown类self向MyForm类__mForm发出信号,并调用doSomething。但正如您在这行中看到的:button=MyPushButtoni,self,我已经将self-MyForm传递给了MyPushButton


我希望这能对你有所帮助。

可能重复的@ardoi:是的,我写了一个太可能重复的@ardoi:是的,我也写了一个