PDA

View Full Version : Connecting dynamically added properties



stefkeB
4th December 2008, 11:15
EDITED: shorter example
-------------------------------
I know it is possible to dynamically add properties to QObject derived classes.


QObject * one = new QObject();
one->setProperty("prop1", QVariant("value1"));
qDebug() << QVariant(one->property("prop1"));

Suppose I have a second class "two", with a similar property (having the same data type).
But I was wondering how to connect both properties using signals and slots?

Should I connect the QObject::setProperty()method? I fear that this would probably mean that setting any property on the first class, would automatically create the same property on the second class, which is clearly not the intention. I only want to ensure that if I connect two properties, setting the first will set the second one automatically.
I will first try to do this in one direction and worry about recursion later on.

Edited:
I created a wrapper method as a slot, "setValue" and a signal "valueChanged", which works, but as a result, if I connect two classes, all properties with the same name get connected. I only want to allow specific connections.
What I would like to avoid is creating a list of mapped properties (e.g. listener pattern) since I fear that I would create something that resembles the signal and slot mechanism, which is clearly a waste of efforts. I would rather use what is already implemented in Qt.

I read about "QDynamicPropertyChangeEvent". Is that an approach to it?

stefkeB
5th December 2008, 15:19
Does anybody believe that the QSignalMapper class could help me with the correct identification and parametrization of the signal?

I want to ensure that only particular connections are enabled, e.g. "prop3" from "object1" to connect to "prop5" from "object2".

My current attempt connects ALL props from one object to the other, based on the name. So it also creates connections which I don't want.

I fear that by creating a separate class for Connections and keeping lists of connections will eventually require me to fully implement a signal/slot system or the observer/listener pattern, while I prefer to use Qt's signal/slot mechanism (as it is probably more robust, more efficient and better documented than what I would create anyway).

Any hints?