PyQt4 信号和槽用法总结

来源:互联网 发布:台湾的网络直播平台17 编辑:程序博客网 时间:2024/05/22 13:53
原文地址

原文 http://neteue.com/article/pyqt4-signal-slot/

信号与槽的例子

可以使用QObject.connect方法来连接信号与槽

#! /usr/bin/env python# -*- coding: utf-8 -*-from PyQt4 import QtCore, QtGuiclass MyWidget(QtGui.QWidget):  def __init__(self, parent=None):      super(MyWidget, self).__init__(parent)      self.quit_button = QtGui.QPushButton("Quit", self)      layout = QtGui.QHBoxLayout()      layout.addWidget(self.quit_button)      self.setLayout(layout)      self.connect(self.quit_button, QtCore.SIGNAL("clicked()"),                   QtGui.qApp, QtCore.SLOT("quit()"))if __name__ == '__main__':  import sys  app = QtGui.QApplication(sys.argv)  win = MyWidget()  win.show()  sys.exit( app.exec_() )
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

在这个例子中, 使用了标准的连接方式connect(发送者, 信号, 接收者, 槽), Qt中的槽可以这样理解: 是接收者中经过包装后某个方法

自定义槽(slot)

当然可以通过QtCore.pyqtSlot自义一个槽, 在旧式风格中使用是的pyqtSignature, 建议使用pyqtSlot

#! /usr/bin/env python# -*- coding: utf-8 -*-from PyQt4 import QtCore, QtGuiclass MyWidget(QtGui.QWidget):    def __init__(self, parent=None):        super(MyWidget, self).__init__(parent)        self.ok_button = QtGui.QPushButton("OK", self)        layout = QtGui.QHBoxLayout()        layout.addWidget(self.ok_button)        self.setLayout(layout)        self.connect(self.ok_button, QtCore.SIGNAL('clicked()'),                      self, QtCore.SLOT("on_ok_button_clicked()"))    @QtCore.pyqtSlot()        def on_ok_button_clicked(self):        print "OK"if __name__ == "__main__":            import sys    app = QtGui.QApplication(sys.argv)    w = MyWidget()    w.show()    app.exec_()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

发送信号

单击按钮后触发emit_python_list信号, 并且执行相应的槽

#! /usr/bin/env python# -*- coding: utf-8 -*-from PyQt4 import QtCore, QtGuiclass MyWidget(QtGui.QWidget):    def __init__(self, parent=None):        super(MyWidget, self).__init__(parent)        self.ok_button = QtGui.QPushButton("OK", self)        layout = QtGui.QHBoxLayout()        layout.addWidget(self.ok_button)        self.setLayout(layout)        self.connect(self.ok_button, QtCore.SIGNAL("clicked()"), self, QtCore.SLOT("slot1()"))        self.connect(self, QtCore.SIGNAL('emit_python_list(PyQt_PyObject)'),                      self, QtCore.SLOT("slot2(PyQt_PyObject)"))    @QtCore.pyqtSlot()    def on_button_clicked(self):            print "click me!!!!"    @QtCore.pyqtSlot()        def slot1(self):        self.emit(QtCore.SIGNAL("emit_python_list(PyQt_PyObject)"), [1, 2, 3, 4, 5, 6])    @QtCore.pyqtSlot("PyQt_PyObject")        def slot2(self, alist):        print alistif __name__ == "__main__":            import sys    app = QtGui.QApplication(sys.argv)    w = MyWidget()    w.show()    app.exec_()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

执行Python方法

这里使用更加优雅直接的方式连接信号并执行回调, 这也是项目中用到最多的方式

#! /usr/bin/env python# -*- coding: utf-8 -*-from PyQt4 import QtCore, QtGuiclass MyWidget(QtGui.QWidget):    def __init__(sel`f, parent=None):        super(MyWidget, self).__init__(parent)        self.ok_button = QtGui.QPushButton("ok", self)        layout = QtGui.QHBoxLayout()        layout.addWidget(self.ok_button)        self.setLayout(layout)        self.ok_button.clicked.connect(self.on_ok_button_clicked)    def on_ok_button_clicked(self):        print "OK"if __name__ == '__main__':  import sys  app = QtGui.QApplication(sys.argv)  win = MyWidget()  win.show()  sys.exit( app.exec_() )
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

self.ok_button.clicked.connect 其中clicked是信号

注册信号

#! /usr/bin/env python# -*- coding: utf-8 -*-from PyQt4 import QtCore, QtGuiclass MyWidget(QtGui.QWidget):    emit_python_list = QtCore.pyqtSignal(object)    def __init__(self, parent=None):        super(MyWidget, self).__init__(parent)        self.ok_button = QtGui.QPushButton("OK", self)        layout = QtGui.QHBoxLayout()        layout.addWidget(self.ok_button)        self.setLayout(layout)        self.ok_button.clicked.connect(self.slot1)        self.emit_python_list.connect(self.slot2)    def slot1(self):            self.emit_python_list.emit([1, 2, 3, 4, 5, 6])    def slot2(self, alist):            print alistif __name__ == "__main__":            import sys    app = QtGui.QApplication(sys.argv)    w = MyWidget()    w.show()    app.exec_()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

自动连接

QMetaObject. connectSlotsByName(QObject)

其作用是如其名称一样,用来将QObject 里的子孙QObject的某些信号按照其objectName连接到相应的槽上,

如是使用pyuic生成的代码, 就是使用这种方式

#! /usr/bin/env python# -*- coding: utf-8 -*-from PyQt4 import QtCore, QtGuiclass MyWidget(QtGui.QWidget):    def __init__(self, parent=None):        super(MyWidget, self).__init__(parent)        self.ok_button = QtGui.QPushButton("OK", self)        self.ok_button.setObjectName("ok_button")        layout = QtGui.QHBoxLayout()        layout.addWidget(self.ok_button)        self.setLayout(layout)        QtCore.QMetaObject.connectSlotsByName(self)    @QtCore.pyqtSlot()         # 一定要有,不然会打印两遍 "OK"     def on_ok_button_clicked(self):        print "OK"if __name__ == "__main__":            import sys    app = QtGui.QApplication(sys.argv)    w = MyWidget()    w.show()    app.exec_()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

别忘了, 使用setObjectName设置对象名称

0 0