PDA

View Full Version : Keeping track of columns in custom models



darkadept
28th February 2008, 19:55
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:


model->setData(model->index(row, 4), value); //4 is the Name column


2. Using enums:


class MyModel : public QSqlTableModel {
Q_OBJECT
public:
enum Columns {
ColumnFirstName = 0,
ColumnLastName,
ColumnAddress,
ColumnCount
};
//I use the last item, ColumnCount, to return in the columnCount(...) method

//class methods...
};

and then:


model->setData(model->index(row, MyModel::ColumnFirstName), value);


3. Using some methods to pass QString's as column names (more useful in QSqlTableModel's I'd think)


int nameColumn = model->getColumn("name");
model->setData(model->index(row, nameColumn), value);


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

wysota
28th February 2008, 20:29
I personally tend to use enums.