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
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:
Q_OBJECT
public:
enum Columns {
ColumnFirstName = 0,
ColumnLastName,
ColumnAddress,
ColumnCount
};
//I use the last item, ColumnCount, to return in the columnCount(...) method
//class methods...
};
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...
};
To copy to clipboard, switch view to plain text mode
and then:
model->setData(model->index(row, MyModel::ColumnFirstName), value);
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)
int nameColumn = model->getColumn("name");
model->setData(model->index(row, nameColumn), value);
int nameColumn = model->getColumn("name");
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
Bookmarks