Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define pyqt4 signals with a list as argument

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.

like image 603
student Avatar asked Dec 23 '12 11:12

student


People also ask

What are signals in PyQt?

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.

What does pyqtSlot() do?

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.

What are signals and slots in Python?

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 .


1 Answers

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.

like image 188
Vicent Avatar answered Oct 04 '22 07:10

Vicent