you mean like following?

Qt Code:
  1. class SqlQueryModel: public QSqlQueryModel
  2. {
  3. Q_OBJECT
  4. QHash<int,QByteArray> hash;
  5. public:
  6. explicit SqlQueryModel() : QSqlQueryModel()
  7. {
  8. //hash = new QHash<int,QByteArray>;
  9. hash.insert(Qt::UserRole, QByteArray("name"));
  10. hash.insert(Qt::UserRole + 1, QByteArray("title"));
  11. }
  12. QVariant data(const QModelIndex &index, int role) const
  13. {
  14. if(role < Qt::UserRole) {
  15. return QSqlQueryModel::data(index, role);
  16. }
  17. QSqlRecord r = record(index.row());
  18. return r.value(QString(hash.value(role))).toString();
  19. }
  20. inline QHash<int, QByteArray> roleNames() const { return hash; }
  21. };
To copy to clipboard, switch view to plain text mode