Results 1 to 4 of 4

Thread: Properties, MetaObjects, and Models

  1. #1
    Join Date
    Dec 2006
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Question Properties, MetaObjects, and Models

    I've been experimenting with the model/view programming methodology and hope soon to fully understand it's implementation in Qt. Suppose that you have a class, say Customer, and that class has Q_PROPERTYs (like firstName, lastName, etc..). Now you want to hold an Address Book, so you make a model.

    First Question: do you subclass QAbstractListModel, or QAbstractTableModel?

    I chose to subclass the table model, and hold the records in rows (QList<Customer*> m_list), and the properties of each Customer in the columns. In the interests of future extendability, I'd like for the model's implementation of columnCount() to return the number of properties available for the class Customer. So this exercise was to see if I colud write a model that would automatically account for any changes made to class Customer. That is; if I change class Customer by addition or removal of properties, I don't want to have to change the model.

    Qt Code:
    1. int myModel::columnCount() {
    2. // we have to subtract the properties gained through inheritance
    3. // from the total count
    4. static int ret = Customer::staticMetaObject()->propertyCount()
    5. - Customer::staticMetaObject->propertyOffset();
    6. return ret;
    7. }
    8.  
    9. int myModel::rowCount() {
    10. return m_list()->size();
    11. }
    To copy to clipboard, switch view to plain text mode 

    Now in order to retrieve our data, we have to reimplement the data function. But this is where I ran into a bit of trouble.
    Qt Code:
    1. QVariant BrushModel::data( const QModelIndex &index, int role ) const {
    2. if ( index.isValid() )
    3. return QVariant();
    4. if ( index.row() < 0 || rowCount() <= index.row() )
    5. return QVariant();
    6. if ( index.column < 0 || columnCount() <= index.column() )
    7. return QVariant();
    8.  
    9. if ( role == Qt::DisplayRole ) {
    10. int col = index.column() + Customer::staticMetaObject()->propertyOffset();
    11. Customer *c = m_list.at( index.row() );
    12. char *name = ??? // somehow we have to map col into a property name
    13. return c->property( name );
    14. }
    15.  
    16. return QVariant();
    17. }
    To copy to clipboard, switch view to plain text mode 

    Perhaps this is just a flawed design and there are better ways to go about this. I may have overlooked something in the documentation surrounding metaobects but I could not find a way to map from an index to a property name. I did find an indexOfProperty(char *name) function, but nothing which goes the other direction.

  2. #2
    Join Date
    Jan 2006
    Location
    Alingsås, Sweden
    Posts
    437
    Thanks
    3
    Thanked 39 Times in 39 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Properties, MetaObjects, and Models

    Can't you use QMetaObject:roperty( int )->name() for this?

    Regarding the design - it looks quite elegant. I did something similar recently to use QObjects to represent a tree structure for a model.

  3. The following user says thank you to e8johan for this useful post:

    EricTheFruitbat (26th January 2007)

  4. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Properties, MetaObjects, and Models

    Quote Originally Posted by EricTheFruitbat View Post
    First Question: do you subclass QAbstractListModel, or QAbstractTableModel?
    It doesn't really matter. You can always transform one representation into the other using a simple proxy model. I guess for manipulating the list representation is better (as you have all information about the object in one place) - it's according "to the book", but a table representation might be better if you wanted to display the model and let the user change values of each property/column.

  5. #4
    Join Date
    Dec 2006
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Properties, MetaObjects, and Models

    Quote Originally Posted by e8johan View Post
    Can't you use QMetaObject::property( int )->name() for this?

    Regarding the design - it looks quite elegant. I did something similar recently to use QObjects to represent a tree structure for a model.
    I knew that I must've been overlooking something.

    And thank you both for the feedback about the design.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.