Qt 5.10 Book Examples
IntListModel.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // IntListModel.cpp
3 // ======================================================================
4 // This file is a part of the book
5 // "Qt 5.10 Professional programming with C++"
6 // http://qt-book.com
7 // ======================================================================
8 // Copyright (c) 2017 by Max Schlee
9 // ======================================================================
10 
11 #include <QtGui>
12 #include "IntListModel.h"
13 
14 // ----------------------------------------------------------------------
15 IntListModel::IntListModel(const QList<int>& list, QObject* pobj/*=0*/)
16  : QAbstractListModel(pobj)
17  , m_list(list)
18 {
19 }
20 
21 // ----------------------------------------------------------------------
22 QVariant IntListModel::data(const QModelIndex& index, int nRole) const
23 {
24  if (!index.isValid()) {
25  return QVariant();
26  }
27  /* if (index.row() < 0 || index.row() >= m_list.size()) {
28  return QVariant();
29  }*/
30  return (nRole == Qt::DisplayRole || nRole == Qt::EditRole)
31  ? m_list.at(index.row())
32  : QVariant();
33 }
34 
35 // ----------------------------------------------------------------------
36 bool IntListModel::setData(const QModelIndex& index,
37  const QVariant& value,
38  int nRole
39  )
40 {
41  if (index.isValid() && nRole == Qt::EditRole) {
42  m_list.replace(index.row(), value.value<int>());
43  emit dataChanged(index, index);
44  return true;
45  }
46  return false;
47 }
48 
49 // ----------------------------------------------------------------------
50 int IntListModel::rowCount(const QModelIndex& parent/*=QModelIndex()*/
51  ) const
52 {
53  if (parent.isValid()) {
54  return 0;
55  }
56 
57  return m_list.size();
58 }
59 
60 // ----------------------------------------------------------------------
61 QVariant IntListModel::headerData(int nSection,
62  Qt::Orientation orientation,
63  int nRole/*=DisplayRole*/
64  ) const
65 {
66  if (nRole != Qt::DisplayRole) {
67  return QVariant();
68  }
69  return (orientation == Qt::Horizontal) ? QString("Number")
70  : QString::number(nSection);
71 }
72 
73 // ----------------------------------------------------------------------
74 Qt::ItemFlags IntListModel::flags(const QModelIndex& index) const
75 {
76  Qt::ItemFlags flags = QAbstractListModel::flags(index);
77  return index.isValid() ? (flags | Qt::ItemIsEditable)
78  : flags;
79 }
80 
81 // ----------------------------------------------------------------------
82 bool IntListModel::insertRows(int nRow,
83  int nCount,
84  const QModelIndex& parent/*=QModelIndex()*/
85  )
86 {
87  if (parent.isValid()) {
88  return false;
89  }
90 
91  beginInsertRows(QModelIndex(), nRow, nRow + nCount - 1);
92  for (int i = 0; i < nCount; ++i) {
93  m_list.insert(nRow, 0);
94  }
95  endInsertRows();
96 
97  return true;
98 }
99 
100 // ----------------------------------------------------------------------
102  int nCount,
103  const QModelIndex& parent/*=QModelIndex()*/
104  )
105 {
106  if (parent.isValid()) {
107  return false;
108  }
109 
110  beginRemoveRows(QModelIndex(), nRow, nRow + nCount - 1);
111  for (int i = 0; i < nCount; ++i) {
112  m_list.removeAt(nRow);
113  }
114  endRemoveRows();
115 
116  return true;
117 }
Qt::ItemFlags flags(const QModelIndex &index) const
bool insertRows(int nRow, int nCount, const QModelIndex &parent=QModelIndex())
bool setData(const QModelIndex &index, const QVariant &value, int nRole)
QVariant data(const QModelIndex &index, int nRole) const
IntListModel(const QList< int > &list, QObject *pobj=0)
bool removeRows(int nRow, int nCount, const QModelIndex &parent=QModelIndex())
QVariant headerData(int nSection, Qt::Orientation orientation, int nRole=Qt::DisplayRole) const
int rowCount(const QModelIndex &parent=QModelIndex()) const