PDA

View Full Version : hmm..any way to generically link one object to update others that inherit it?



Thomas Wrobel
30th January 2010, 22:31
Ok, I'm a bit of a newbie so I'm not sure if this is possible, but if it is its probably going to be a fairly advanced answer, so hence I posted here.

I've basically got my own object type "arblip", this contains a number of data types. (Stings and doubles mostly).
I'm also making a set of widgets to display this data in various forms (say, a list that takes arblips and displays selected data from them)

The arblips themselves are stored in a array.

Is there any way of creating the widgets &or blip objects in such a way that when a piece of data in one is changed, it will update all the other widgets that use that arblip as a datasource?
(so, it possibly might involve the arblip object triggering a inherited list of slots when a setter is triggered for it?)

I can of course do this manually, but as I might end up with quite a few custom widgets it seems nicer if there was a way to organsie my object/data structure for this to be preformed neatly.

Thanks for any help in advance, and I hope I explained myself clearly enough,

Thomas

JohannesMunk
31st January 2010, 01:22
Hi there!

You make it sound, as if the traditional signal slot mechanism would not be a satisfactory solution for you! But why?

I describe it anyway: You need to make your arblip a QObject descendant, call the Q_OBJECT macro. Define a signal DataChanged. (Or you may want more granularity and define separate signals for different values that changed.) In each property setter you call "emit DataChanged();" or the respective signal.

Whenever you "add" an arblip to a displaywidget (this), you just connect those:

connect(arblip,SIGNAL(DataChanged()),this,Update() );

There is no problem in connecting the same signal multiple times to different widgets..

Where exactly is this not what you need or to cumbersome?

You are speaking of linking to objects that inherit it. Do your displaywidgets inherit your Dataobject??

HIH

Johannes

wysota
31st January 2010, 02:27
I think the model-view architecture is perfect for you. You just need to wrap your data in QAbstractItemModel interface (you can use QStandardItemModel or implement your own custom model) and then use one of the standard views or QDataWidgetMapper. If you require filtering or sorting, you can use QSortFilterProxyModel along with all the mentioned classes.

Thomas Wrobel
4th February 2010, 15:47
Thanks for both your suggestions.

You make it sound, as if the traditional signal slot mechanism would not be a satisfactory solution for you! But why?

No, it is perfect, I just needed someone to explain how to use it for this purpose...which you did :)
I'll probably use the method as you described for now.

Although wysota suggestion of using a model-view might be better for me in future.

Thanks for both your help :)

JohannesMunk
4th February 2010, 16:21
Ok. Probably you know it, but I missed the SLOT() around the slot in the connect statement :->

Let me know, if you need more advice!

Johannes