Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef NEWSSQLMODEL
- #define NEWSSQLMODEL
- #include <QSqlQueryModel>
- class NewsSqlModel : public QSqlQueryModel
- {
- Q_OBJECT
- public:
- explicit NewsSqlModel(QObject *parent);
- void refresh();
- QVariant data(const QModelIndex &index, int role) const;
- private:
- const static char* COLUMN_NAMES[];
- const static char* SQL_SELECT;
- };
- const char* NewsSqlModel::COLUMN_NAMES[] = {
- "title",
- "text"
- };
- const char* NewsSqlModel::SQL_SELECT =
- "SELECT title, text FROM news";
- NewsSqlModel::NewsSqlModel(QObject *parent) :
- QSqlQueryModel(parent)
- {
- int idx = 0;
- QHash<int, QByteArray> roleNames;
- while( COLUMN_NAMES[idx]) {
- roleNames[Qt::UserRole + idx + 1] = COLUMN_NAMES[idx];
- idx++;
- }
- // setRoleNames(roleNames);
- refresh();
- }
- QVariant NewsSqlModel::data(const QModelIndex &index, int role) const
- {
- QVariant value = QSqlQueryModel::data(index, role);
- if(role < Qt::UserRole)
- {
- value = QSqlQueryModel::data(index, role);
- }
- else
- {
- int columnIdx = role - Qt::UserRole - 1;
- QModelIndex modelIndex = this->index(index.row(), columnIdx);
- value = QSqlQueryModel::data(modelIndex, Qt::DisplayRole);
- }
- return value;
- }
- void NewsSqlModel::refresh()
- {
- this->setQuery(SQL_SELECT);
- }
- #endif // NEWSSQLMODEL
Advertisement
Add Comment
Please, Sign In to add comment