PDA

View Full Version : reacting to changes for non-QObject classes?



jajdoo
8th August 2011, 10:12
i have a few clients reading from the same data holder. what method would you recommend using to make sure they're all informed when data changes for an object not able to emit signals?

stampede
8th August 2011, 12:04
Observer pattern (http://en.wikipedia.org/wiki/Observer_pattern), you can find a lot more on that subject.

jajdoo
8th August 2011, 16:40
would it be a good idea to use Qt's event system for that?

wysota
8th August 2011, 17:06
In theory yes, but it requires that the (receiving) object inherits QObject and then you can use signals and slots instead.

jajdoo
8th August 2011, 18:21
the sending element cant be a QObject, then this might be a good dir

wysota
8th August 2011, 19:16
Still you can use a has-a relationship with a QObject instead of an is-a relation. Just like QFuture and QFutureWatcher are related.

jajdoo
9th August 2011, 09:20
im not quite sure i know what you meant.. who has whom?
anyhow, say i post an event - who is the receiver? will it reach the rest of the gui if i send it to the main window?

wysota
9th August 2011, 10:15
im not quite sure i know what you meant.. who has whom?

You can look into Qt's source code but I suspect it's something like this:


class Watcher;
class Object {
private:
QList<Watcher*> m_watchers;
friend class Watcher;
void notify();
};
class Watcher : public QObject {
Q_OBJECT
public:
Watcher(Object *obj, QObject *parent = 0) : QObject(parent) { obj->m_watchers << this; }
signals:
void objectChanged(Object*);
friend class Object;
};

void Object::notify() {
foreach(Watcher *w, m_watchers) emit w->objectChanged(this);
}


anyhow, say i post an event - who is the receiver?
Every object that wanted to receive notifications.


will it reach the rest of the gui if i send it to the main window?
No, unless each interested object installs an event filter on the main window object.