Results 1 to 2 of 2

Thread: Keeping track of columns in custom models

  1. #1
    Join Date
    May 2006
    Posts
    70
    Thanks
    12
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Keeping track of columns in custom models

    I have a lot of models in my application. Some are normal QSqlTableModel's, some are sub-classed QSqlTableModel's, and other are sub-classed from QAbstractTableModel's.

    What's the best/favorite/nicest way to keep track of columns in a model?

    Here are some options that I thought of:

    1. Hard-coding the column number:
    Qt Code:
    1. model->setData(model->index(row, 4), value); //4 is the Name column
    To copy to clipboard, switch view to plain text mode 

    2. Using enums:
    Qt Code:
    1. class MyModel : public QSqlTableModel {
    2. Q_OBJECT
    3. public:
    4. enum Columns {
    5. ColumnFirstName = 0,
    6. ColumnLastName,
    7. ColumnAddress,
    8. ColumnCount
    9. };
    10. //I use the last item, ColumnCount, to return in the columnCount(...) method
    11.  
    12. //class methods...
    13. };
    To copy to clipboard, switch view to plain text mode 
    and then:
    Qt Code:
    1. model->setData(model->index(row, MyModel::ColumnFirstName), value);
    To copy to clipboard, switch view to plain text mode 

    3. Using some methods to pass QString's as column names (more useful in QSqlTableModel's I'd think)
    Qt Code:
    1. int nameColumn = model->getColumn("name");
    2. model->setData(model->index(row, nameColumn), value);
    To copy to clipboard, switch view to plain text mode 

    I'm open to any more ideas you might have. I'm trying to find a balance between not depending too much on the database (how do we know which order the columns are when using a QSqlTableModel?) and ease of use.

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: Keeping track of columns in custom models

    I personally tend to use enums.

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.