User Tools

Site Tools


info:qt_readonlytablemodel

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

info:qt_readonlytablemodel [2009-06-02 14:35] (current)
sam created
Line 1: Line 1:
 +====== ReadOnlyTableModel.h ======
 +<code cpp>
 +/*
 + * ReadOnlyTableModel.h
 + *
 + ​* ​ Created on: May 19, 2009
 + ​* ​     Author: sam
 + */
 +
 +#ifndef READONLYTABLEMODEL_H_
 +#define READONLYTABLEMODEL_H_
 +
 +#include <​QSqlTableModel>​
 +
 +/**
 + * This class is a very simple extension of QSqlTableModel that makes all
 + * fields read-only.
 + */
 +class ReadOnlyTableModel : public QSqlTableModel {
 + Q_OBJECT
 +
 +public:
 + explicit ReadOnlyTableModel(QObject *parent = 0, QSqlDatabase db =
 + QSqlDatabase());​
 +
 + Qt::​ItemFlags flags(const QModelIndex&​ index) const;
 + bool setData(const QModelIndex &index, const QVariant &value, int role);
 +};
 +
 +/**
 + * Since this model is read-only, we must always return false.
 + */
 +inline bool ReadOnlyTableModel::​setData(const QModelIndex &index,
 + const QVariant &value, int role) {
 + return false;
 +}
 +
 +#endif /* READONLYTABLEMODEL_H_ */
 +</​code>​
 +
 +====== ReadOnlyTableModel.cpp ======
 +<code cpp>
 +/*
 + * ReadOnlyTableModel.cpp
 + *
 + ​* ​ Created on: May 19, 2009
 + ​* ​     Author: sam
 + */
 +
 +#include "​ReadOnlyTableModel.h"​
 +
 +ReadOnlyTableModel::​ReadOnlyTableModel(QObject *parent,
 + QSqlDatabase db) : QSqlTableModel(parent,​ db)
 +{
 +}
 +
 +Qt::​ItemFlags ReadOnlyTableModel::​flags(const QModelIndex & index) const
 +{
 + return Qt::​ItemIsSelectable | Qt::​ItemIsEnabled;​
 +}
 +</​code>​
  
info/qt_readonlytablemodel.txt · Last modified: 2009-06-02 14:35 by sam