[Interest] Representing to-many relationships in property system?

Chris Meyer cmeyer1969+qt at gmail.com
Sat Oct 13 02:41:07 CEST 2012


On Thu, Oct 11, 2012 at 4:25 AM, Nikos Chantziaras <realnc at gmail.com> wrote:
> On 11/10/12 01:07, Chris Meyer wrote:
>> The Qt property system easily represents properties (scalars) and
>> to-one relationships (objects) using the Q_PROPERTY macro.
>
> No, not really.  This isn't SQL.  It's C++.  You cannot just declare
> relationships.  You will need to implement them on your own.
>
>
>> However, what is the best way to represent to-many relationships?
>>
>> For instance, I can represent have an Employee and define a property
>> for the employee's name (scalar string) and his company (object). But
>> if I define a Company, how to I provide access to the company's
>> employees (list of objects)?
>
> There are multiple ways to implement this.  The most straightforward is
> to have the employee's constructor add the object to a list maintained
> by Company, and remove itself from that list in the destructor.
>
> But this sounds fishy.  If you need to maintain data on employees and
> companies, you either store that as data on disk using your own format,
> or use a database.  You do *not* design a C++ class hierarchy around it.

I've used a C++ system for years that includes relationship
information. It would be nice to include this in the Qt meta system.
For instance:

Q_TO_ONE(Company company READ company NOTIFY companyChanged)
Q_TO_MANY(Person employees COUNT employeeCount READ employeeAt INSERT
insertEmployee REMOVE removeEmployee)

Then Qml, scripts, and other metadata clients could have standardized
access to the relationships.

Apple's Key Value Coding system does just this... it's very similar to
Qt's meta property system except it has two features which make it
more useful:

1) Includes support for to-Many relationships.

2) Change methods include 'aboutToChange' and 'didChange'
functionality rather than just 'changed'.

The combination of these two allow you to represent a wide variety of
data models; and observe everything that happens to them (for
instance, I use a similar system for recording undo states in my
model).

Here a pointer to current KVC documentation. It's worthwhile to read it.

http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/KeyValueCoding/Articles/BasicPrinciples.html



More information about the Interest mailing list