В мануале Qt, есть такой пример по моделям:
Код | class StringListModel : public QAbstractListModel { Q_OBJECT
public: StringListModel(const QStringList &strings, QObject *parent = 0) : QAbstractListModel(parent), stringList(strings) {}
//нету нигде virtual
int rowCount(const QModelIndex &parent = QModelIndex()) const; QVariant data(const QModelIndex &index, int role) const; QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
private: QStringList stringList; };
int StringListModel::rowCount(const QModelIndex &parent) const { return stringList.count(); }
QVariant StringListModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) return QVariant();
if (index.row() >= stringList.size()) return QVariant();
if (role == Qt::DisplayRole) return stringList.at(index.row()); else return QVariant(); }
QVariant StringListModel::headerData(int section, Qt::Orientation orientation, int role) const { if (role != Qt::DisplayRole) return QVariant();
if (orientation == Qt::Horizontal) return QString("Column %1").arg(section); else return QString("Row %1").arg(section); }
|
ReadOnly - модель, чтобы сделать её редактируемую вместо того, чтобы создавать свою функцию лучше переопределить функции:
Код | Qt::ItemFlags flags(const QModelIndex &index) const; //здесь можно установить флаг, на то что элемент по index'у можно изменять //(во view doubleclick и пошло редактирование) bool setData(const QModelIndex &index, const QVariant &value, //завершили редактирование, обновили данные int role = Qt::EditRole);
//вставка, удаление строк в модели bool insertRows(int position, int rows, const QModelIndex &index = QModelIndex()); bool removeRows(int position, int rows, const QModelIndex &index = QModelIndex());
|
|