2
votes

PySide and PyQt employ Qt signal-slot mechanism with which we can connect any/multiple signals to any/multiple slots as far as the trasmistted data types match.

The signalling object has some knowledge of the receiving slots, e.g. knows their number via method receivers or the signal can disconnect from the receiving slots via its disconnect method.

My problem relates to the oposite direction - e.g. does a slot know to which signals it is connected? Can a slot disconnect from the signals?

UPDATE: So why I am asking this - I have an object that performs some calculation. The calculation is defined by a user editable Python expression. The expression is parsed and necessary data sources are identified from this... The calculation object (acts as a slot) then connects to these data sources (they act as signals) and once the data sources produce/update a value, this fact is signalled to the slot and the expression is reevaluated. And when the expression is changed by a user, it needs to be parsed again and disconnected from the existing signals (i.e. data sources) and connect to new data sources. You can imagine it is something like a formula in Excel that is connected to other cells.

I know there are a few ways to work around this, e.g. keeping track of connections manually (well, this is extra work) or deleting the expression object and creating a new one everytime it is changed (seems not good enough, because user might want to trace back the calculation data sources and this will not help). But I was curious if this can be solved purely using simple signal-slot mechanism. In other words, I am not interested in any workarounds... I know of them and will use them I signals-slots will not help here. :)

1
I don't think so. However you can track connections and disconnections manually (for example, using a wrapper of QObject::connect).Pavel Strakhov
I think there must be something, because when a receiving object is deleted, the signal must be notified that one of its receivers is dead to stop sending signals to it. But I have not found anything in the docs.... Of course, keeping the track manually is my other alternative. However I would like to avoid making double work if Qt could do this for me automatically.V.K.
I think you are asking wrong question. Please explain what problem are you trying to solve, do not ask how to fix your idea of solutions to some mysterious problem. This is quite common mistake when asking for help. Why you need to know where slot is connected? Maybe in this case you need temporary disconnect signal to prevent recursion? To prevent possible recursion there is blockSignals method.Marek R
I have updated the question to explain my intentions.V.K.
You can save QMetaObject::Connection objects returned by QObject::connect and use them in QObject::disconnect. It's not so much extra work.Pavel Strakhov

1 Answers

3
votes

The approach you propose forces a very close relationship between the concrete data widgets and the calculation engine. You mingle UI with the calculations. This makes it much harder than it needs to be.

What you could try instead is the model-view approach. The model would be a simple implementation of QAbstractTableModel. The view would be the individual data-entry widgets mapped to the model's cells using QDataWidgetMapper. The calculation engine would access only the model, completely unaware of how the model is modified by the widgets. This make life easier.

The calculation object can connect to the model using just one dataChanged signal and it will be notified of changes to any of the variables. You can easily pass both the value and the variable name by having two columns in the table.

The implementation of the model can be very simple, you can have a list of strings for the variable names in the first column, and a list of variants for the second column. The model must correctly emit the dataChanged signal whenever setData is called, of course.