solved like following. thanks!
#ifndef SQLQUERYMODEL_H
#define SQLQUERYMODEL_H
#include <QtSql>
#include <QAbstractItemModel>
{
Q_OBJECT
QHash<int,QByteArray> *hash;
public:
{
hash = new QHash<int,QByteArray>;
hash
->insert
(Qt
::UserRole + 1,
QByteArray("title"));
}
{
if(role < Qt::UserRole) {
}
return r.
value(QString(hash
->value
(role
))).
toString();
}
inline QHash<int, QByteArray> roleNames() const { return *hash; }
};
#ifndef SQLQUERYMODEL_H
#define SQLQUERYMODEL_H
#include <QtSql>
#include <QAbstractItemModel>
class SqlQueryModel: public QSqlQueryModel
{
Q_OBJECT
QHash<int,QByteArray> *hash;
public:
explicit SqlQueryModel() : QSqlQueryModel()
{
hash = new QHash<int,QByteArray>;
hash->insert(Qt::UserRole, QByteArray("name"));
hash->insert(Qt::UserRole + 1, QByteArray("title"));
}
QVariant data(const QModelIndex &index, int role) const
{
if(role < Qt::UserRole) {
return QSqlQueryModel::data(index, role);
}
QSqlRecord r = record(index.row());
return r.value(QString(hash->value(role))).toString();
}
inline QHash<int, QByteArray> roleNames() const { return *hash; }
};
To copy to clipboard, switch view to plain text mode
#endif // SQLQUERYMODEL_H
Bookmarks