[PyQt] Autoconnecting two signals to the same method.

Alexandr N Zamaraev tonal at promsoft.ru
Mon Mar 17 06:15:05 GMT 2008


Why do not use QSignalMapper?

Also simple wrote "multy" decorator.
Example:
   @multyNamedSignature([
     'on_pushButton1_clicked',
     'on_pushButton2_clicked'])
   @QtCore.pyqtSignature('')
   def on_pushButton_clicked(self):
     print "Hi Man!"

Source:

import sys

from PyQt4 import QtCore, QtGui, uic

def multyNamedSignature(names):
   'Decorator for miltiplicates qtSigned function by name'
   def wrap(signed_func):
     sign = '(' + signed_func.__pyqtSignature__.split('(', 1)[1]
     def re_sign(name):
       from new import function
       f = function(
         signed_func.func_code, signed_func.func_globals, name,
         signed_func.func_defaults)
       f.__pyqtSignature__ = name + sign
       return f

     fns = dict((n, re_sign(n)) for n in names)
     import inspect
     frame = inspect.currentframe()
     try:
       frame.f_back.f_locals.update(fns)
     finally:
       del frame
     return signed_func
   return wrap

(form_class, base_class) = uic.loadUiType("qtui.ui")

class MyForm(form_class, base_class):
   def __init__(self, *args):
     base_class.__init__(self, *args)
     self.setupUi(self)

   @multyNamedSignature([
     'on_pushButton1_clicked', 'on_pushButton2_clicked'])
   @QtCore.pyqtSignature('')
   def on_pushButton_clicked(self):
     print "Hi Man!"

app = QtGui.QApplication(sys.argv)
form = MyForm()
form.show()
app.exec_()





More information about the PyQt mailing list