《Old-style Signal and Slot Support》文档的测试程序

《Old-style Signal and Slot Support》文档的测试程序 

 

#!/usr/bin/env python
#coding:utf8

__version__ = "1.0"

"""
《Old-style Signal and Slot Support》文档的测试程序 
- [email protected] @2013.02.11
http://pyqt.sourceforge.net/Docs/PyQt4/old_style_signals_slots.html

注意:
1、如果Signal、Slot相关代码写错了,它不会报错,只是不能正确运行。
2、建议学学New Style的写法
"""

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import PyQt4.QtCore as QtCore
import PyQt4.QtGui as QtGui

from PyQt4.QtCore import QVariant

import os, sys, time

from signalDialog import *

def pyFunction():
    print "pyFunction"

def pyFunction2(i):
    print "pyFunction", i

class TestDialog(QDialog):

    def __init__(self, parent=None):
        super(TestDialog, self).__init__(parent)

        self.ui = Ui_Dialog()
        self.ui.setupUi(self)

        #定义:bool QObject.connect (QObject, SIGNAL(), callable, Qt.ConnectionType = Qt.AutoConnection)
        #SIGNAL 可以是 Qt Signal,也可以是PyQt Signal
        #  Qt Signal和Slot,是指C++里面静态定义的
        #callable 可以是函数,也可以是类的方法
        QtCore.QObject.connect(self, QtCore.SIGNAL('QtSig_pyFunc()'), pyFunction)
        QtCore.QObject.connect(self, QtCore.SIGNAL('QtSig_pyMethod()'), self.pyMethod)
        QtCore.QObject.connect(self.ui.scrollBar, SIGNAL('valueChanged(int)'), pyFunction2)

        #定义:bool QObject.connect (QObject, SIGNAL(), QObject, SLOT(), Qt.ConnectionType = Qt.AutoConnection)        
        QtCore.QObject.connect(self.ui.scrollBar, SIGNAL('valueChanged(int)'), self.ui.label,  SLOT('setNum(int)'))
        #也可以这样写
        #定义:bool connect (self, QObject, SIGNAL(), SLOT(), Qt.ConnectionType = Qt.AutoConnection)
        #self.ui.label.connect(self.ui.scrollBar, QtCore.SIGNAL('valueChanged(int)'), SLOT('setNum(int)')) 

        #PySig和QtSlot/PySlot的关联
        QtCore.QObject.connect(self, SIGNAL('PySig_qtSlot(int)'), self.ui.label,  SLOT('setNum(int)'))
        QtCore.QObject.connect(self, QtCore.SIGNAL('PySig_pySlot()'), self, QtCore.SLOT('pySlot()'))


        #用PyQt_PyObject -- 它表示参数可以是任意的Python类型
        QtCore.QObject.connect(self, QtCore.SIGNAL('PySig_PyObject(PyQt_PyObject)'), pyFunction2)

        #Short-circuit Signals
        #它没有参数和括号,但是可以传递任何数量和类型的参数给Slot(传递的参数不足会给警告,多余的会丢弃)
        #它隐形定义了各个参数为 PyQt_PyObject
        #注意只能关联到Python的Slots,不能是Qt Slots
        QtCore.QObject.connect(self, QtCore.SIGNAL('PySig2'), pyFunction2)

        #SIGNAL可以和SIGNAL关联
        QtCore.QObject.connect(self, QtCore.SIGNAL('PySig2Sig'), self, QtCore.SIGNAL('PySig2'))

        #一个Sig可以和多个SLots关联
        #一个Slots可以和多个Sig关联
        #可以在线程之间关联

        self.runTest()

    #注意必须用修饰器
    #@QtCore.pyqtSignature()  #已废弃,改用pyqtSlot
    @pyqtSlot() 
    def pySlot(self):
        print "pySlot"

    def runTest(self):
        self.emit(QtCore.SIGNAL('QtSig_pyFunc()'))
        self.emit(QtCore.SIGNAL('QtSig_pyMethod()'))

        self.emit(QtCore.SIGNAL('PySig_qtSlot(int)'), 2)
        self.emit(QtCore.SIGNAL('PySig_pySlot()'))

        self.emit(QtCore.SIGNAL('PySig_PyObject(PyQt_PyObject)'), ('abc', 'def'))
        self.emit(QtCore.SIGNAL('PySig_PyObject(PyQt_PyObject)'), {'a':1, 'b':2})

        self.emit(QtCore.SIGNAL('PySig2'), 1)
        self.emit(QtCore.SIGNAL('PySig2'), (3, 4))
        self.emit(QtCore.SIGNAL('PySig2Sig'), 5)

    def pyMethod(self):
        print "pyMethod"

    

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    form = TestDialog()
    form.show()
    app.exec_()

 

signalDialog.py:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'signalDialog.ui'
#
# Created: Mon Feb 11 17:34:34 2013
#      by: PyQt4 UI code generator 4.9.6
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(400, 300)
        self.buttonBox = QtGui.QDialogButtonBox(Dialog)
        self.buttonBox.setGeometry(QtCore.QRect(30, 240, 341, 32))
        self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
        self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
        self.buttonBox.setObjectName(_fromUtf8("buttonBox"))
        self.scrollBar = QtGui.QScrollBar(Dialog)
        self.scrollBar.setGeometry(QtCore.QRect(20, 20, 160, 16))
        self.scrollBar.setOrientation(QtCore.Qt.Horizontal)
        self.scrollBar.setObjectName(_fromUtf8("scrollBar"))
        self.label = QtGui.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(20, 50, 161, 61))
        font = QtGui.QFont()
        font.setPointSize(22)
        self.label.setFont(font)
        self.label.setObjectName(_fromUtf8("label"))

        self.retranslateUi(Dialog)
        QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("accepted()")), Dialog.accept)
        QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("rejected()")), Dialog.reject)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
        self.label.setText(_translate("Dialog", "0", None))

 

你可能感兴趣的:(python)