Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyQt signal with arguments of arbitrary type / PyQt_PyObject equivalent for new-style signals

I have an object that should signal that a value has changed by emitting a signal with the new value as an argument. The type of the value can change, and so I'm unsure of how to write the signal type. I know that I can acconmplish this using old-style signals like this:

self.emit(SIGNAL("valueChanged(PyQt_PyObject)"), newvalue)

but how would I write this using new-style signals?

I am aware of a previous question related to this but no "real" answer was given there.

like image 359
pafcu Avatar asked Dec 23 '10 22:12

pafcu


People also ask

What is PyQt signal?

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 is @pyqtSlot?

@pyqtSlot , in turn, is a decorator which converts simple python method to Qt slot. Doc states: Although PyQt5 allows any Python callable to be used as a slot when connecting signals, it is sometimes necessary to explicitly mark a Python method as being a Qt slot and to provide a C++ signature for it.

What is Pyqtboundsignal?

It monitors a group of signals and emits a connected signal whenever it detects an initial connection or a final disconnection: from PyQt5.QtCore import * from PyQt5.QtWidgets import * class ConnectionWatcher(QObject): connected = pyqtSignal(str, bool) def __init__(self, target, *signals): if not target.findChild( ...


1 Answers

First, the object you're emitting from needs the signal defined as an attribute of its class:

class SomeClass(QObject):
    valueChanged = pyqtSignal(object)  

Notice the signal has one argument of type object, which should allow anything to pass through. Then, you should be able to emit the signal from within the class using an argument of any data type:

self.valueChanged.emit(anyObject)
like image 116
Luke Avatar answered Sep 20 '22 23:09

Luke