According to
http://pyqt.sourceforge.net/Docs/PyQt4/new_style_signals_slots.html
I can define a pyqt4-signal with takes an integer argument by mysignal = pyqtSignal(int)
. How can I define a signal which takes an integer and a list of strings or more generally of an object called myobject
as argument.
Each PyQt widget, which is derived from QObject class, is designed to emit 'signal' in response to one or more events. The signal on its own does not perform any action. Instead, it is 'connected' to a 'slot'. The slot can be any callable Python function.
The pyqtSlot() DecoratorDecorate a Python method to create a Qt slot. Parameters: types – the types that define the C++ signature of the slot. Each type may be a Python type object or a string that is the name of a C++ type.
Signals and slots are used for communication between objects. The signals and slots mechanism is a central feature of Qt and probably the part that differs most from the features provided by other frameworks. Signals and slots are made possible by Qt's meta-object system .
The following code creates a signal which takes two arguments: an integers and a list of objects. The UI contains just a button. The signal is emitted when the button is clicked.
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class Foo(object):
pass
class MyWidget(QWidget):
mysignal = pyqtSignal(int, list)
def __init__(self, parent=None):
super(MyWidget, self).__init__(parent)
self.hlayout = QHBoxLayout()
self.setLayout(self.hlayout)
self.b = QPushButton("Emit your signal!", self)
self.hlayout.addWidget(self.b)
self.b.clicked.connect(self.clickHandler)
self.mysignal.connect(self.mySignalHandler)
def clickHandler(self):
self.mysignal.emit(5, ["a", Foo(), 6])
def mySignalHandler(self, n, l):
print n
print l
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
w = MyWidget()
w.show()
sys.exit(app.exec_())
When clicking the button you should see something like:
5
['a', <__main__.Foo object at 0xb7423e0c>, 6]
on your terminal.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With